This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
Embedding project templates in Visual Studio Code extension using Yeoman for templating. #515
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7421d68
Adding Yeoman based generator and environment to VSCode extension.
ricardo-espinoza 65f51d6
Move prompting logic into yeoman generator.
ricardo-espinoza 734d83d
Fixing .vscodeignore file to exclude unnecessary files from the VSIX.
ricardo-espinoza efbc612
Renaming template files to remove unnecessary .txt extension.
ricardo-espinoza 4f4cb28
Updating .gitignore files.
ricardo-espinoza 997240c
Copying the templated files and opening the project
ricardo-espinoza ad067bb
Setting project namespace correctly using the templates.
ricardo-espinoza eed4bd7
Setting path to read template files from VSIX.
ricardo-espinoza e49be05
Name project executable according to desired namespace.
ricardo-espinoza 588fb4a
Merge branch 'master' into ricardoe/templates/yeoman-direct
ricardo-espinoza 55d4641
Merge branch 'master' into ricardoe/templates/yeoman-direct
ricardo-espinoza 435b6fa
Rename command to install templates to clarify they are for CL.
ricardo-espinoza b44604d
Merge branch 'master' into ricardoe/templates/yeoman-direct
ricardo-espinoza fb845e0
Update src/VSCodeExtension/templates/unittest/Tests.qs
ricardo-espinoza e02d4e7
Update src/VSCodeExtension/templates/unittest/Tests.qs
ricardo-espinoza 850da41
Update src/VSCodeExtension/templates/unittest/Tests.qs
ricardo-espinoza 591f865
Update src/VSCodeExtension/templates/library/Library.qs
ricardo-espinoza 2b3db23
Update src/VSCodeExtension/templates/application/Program.qs
ricardo-espinoza dac7140
Update src/VSCodeExtension/templates/application/Program.qs
ricardo-espinoza 4e8986c
Update src/VSCodeExtension/templates/library/Library.qs
ricardo-espinoza 3280d21
Pull request feedback on error handling.
ricardo-espinoza 04b5563
Merge branch 'master' into ricardoe/templates/yeoman-direct
ricardo-espinoza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import * as vscode from 'vscode'; | ||
| import * as fs from 'fs-extra'; | ||
| import * as path from 'path'; | ||
| import yo = require("yeoman-generator"); | ||
| import yosay = require("yosay"); | ||
|
|
||
| export class QSharpGenerator extends yo { | ||
|
|
||
| constructor(args : any, opts : any) { | ||
| super(args, opts); | ||
| console.log( | ||
| yosay("Welcome to the Q# generator!") | ||
| ); | ||
|
|
||
| this.sourceRoot(path.join(this.options.extensionPath, "templates")); | ||
| } | ||
|
|
||
| prompting() { | ||
| let done = this.async(); | ||
|
|
||
| // This dictionary maps the public description of the project type to the name | ||
| // of the folder with the corresponding template files. | ||
| const projectTypes: {[key: string]: string} = { | ||
| "Standalone console application": "application", | ||
| "Quantum library": "library", | ||
| "Unit testing project": "unittest" | ||
| }; | ||
|
|
||
| vscode.window.showQuickPick( | ||
| Object.keys(projectTypes) | ||
| ).then( | ||
| projectTypeSelection => { | ||
| if (projectTypeSelection === undefined) { | ||
| throw undefined; | ||
| } | ||
|
|
||
| vscode.window.showSaveDialog({ | ||
| saveLabel: "Create Project" | ||
| }).then( | ||
| (uri) => { | ||
| if (uri !== undefined) { | ||
| if (uri.scheme !== "file") { | ||
| vscode.window.showErrorMessage( | ||
| "New projects must be saved to the filesystem." | ||
| ); | ||
| throw new Error("URI scheme was not file."); | ||
| } | ||
| else { | ||
| return uri; | ||
| } | ||
| } else { | ||
| throw undefined; | ||
| } | ||
| } | ||
| ) | ||
| .then(uri => { | ||
| this.options.projectType = projectTypes[projectTypeSelection]; | ||
| this.options.outputUri = uri; | ||
| done(); | ||
| }); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| writing() { | ||
| console.log( | ||
| yosay("Creating Q# project.") | ||
| ); | ||
|
|
||
| let sourceDir = path.join(this.templatePath(), this.options.projectType); | ||
| let targetDir = this.options.outputUri.fsPath; | ||
| fs.mkdir(targetDir); | ||
|
|
||
| // Namespace is the directory name itself. | ||
| let dirs = targetDir.split(path.sep); | ||
|
|
||
| // In case there is a trailing separator. | ||
| let namespaceName = dirs.pop() || dirs.pop(); | ||
|
|
||
| fs.readdir(sourceDir, (err, files) => { | ||
| if (err){ | ||
| throw err; | ||
| } | ||
| files.forEach( (filename) => { | ||
| let destinationName = filename; | ||
| let fileExtension = filename.split(".").pop(); | ||
|
|
||
| if (fileExtension && fileExtension.toLowerCase() === "csproj") { | ||
| destinationName = namespaceName + ".csproj"; | ||
| } | ||
|
|
||
| this.fs.copyTpl( | ||
| path.join(sourceDir, filename), | ||
| path.join(targetDir, destinationName), | ||
| { name: namespaceName } | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| const openItem = "Open new project..."; | ||
| vscode.window.showInformationMessage( | ||
| `Successfully created new project at ${targetDir}.`, | ||
| openItem | ||
| ).then( | ||
| (item) => { | ||
| if (item === openItem) { | ||
| vscode.commands.executeCommand( | ||
| "vscode.openFolder", | ||
| this.options.outputUri | ||
| ).then( | ||
| (value) => { }, | ||
| (value) => { | ||
| vscode.window.showErrorMessage("Could not open new project"); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Generated or copied files | ||
| *.csproj |
8 changes: 8 additions & 0 deletions
8
src/VSCodeExtension/templates/application/Application.csproj.v.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <Project Sdk="Microsoft.Quantum.Sdk/#NUGET_VERSION#"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace <%= name %> { | ||
| open Microsoft.Quantum.Canon; | ||
| open Microsoft.Quantum.Intrinsic; | ||
|
|
||
|
|
||
| @EntryPoint() | ||
| operation SayHello() : Unit { | ||
| Message("Hello quantum world!"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Generated or copied files | ||
| *.csproj |
7 changes: 7 additions & 0 deletions
7
src/VSCodeExtension/templates/library/Library.csproj.v.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <Project Sdk="Microsoft.Quantum.Sdk/#NUGET_VERSION#"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.1</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace <%= name %> { | ||
| open Microsoft.Quantum.Canon; | ||
| open Microsoft.Quantum.Intrinsic; | ||
|
|
||
|
|
||
| operation SayHello() : Unit { | ||
| Message("Hello quantum world!"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Generated or copied files | ||
| *.csproj | ||
|
ricardo-espinoza marked this conversation as resolved.
|
||
16 changes: 16 additions & 0 deletions
16
src/VSCodeExtension/templates/unittest/Test.csproj.v.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.Quantum.Sdk/#NUGET_VERSION#"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Quantum.Xunit" Version="#NUGET_VERSION#" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" /> | ||
| <PackageReference Include="xunit" Version="2.4.1" /> | ||
| <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" /> | ||
| <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace <%= name %> { | ||
| open Microsoft.Quantum.Canon; | ||
| open Microsoft.Quantum.Diagnostics; | ||
| open Microsoft.Quantum.Intrinsic; | ||
|
|
||
|
|
||
| @Test("QuantumSimulator") | ||
| operation AllocateQubit() : Unit { | ||
|
|
||
| using (q = Qubit()) { | ||
| Assert([PauliZ], [q], Zero, "Newly allocated qubit must be in |0> state."); | ||
| } | ||
|
|
||
| Message("Test passed."); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.