Skip to content

Commit

Permalink
Searching for csproj, fsproj that uses Microsoft.Net.Sdk.Web (#10704)
Browse files Browse the repository at this point in the history
* Searching for csproj, vbproj, fsproj using sdk Microsoft.Net.Sdk.Web

* Updating package.json

* Updating error message

* Review comments

* Review comments

* Try-catch

* Iteratively find and check for file encodings

* Review comments

* Supporting only utf encodings

* L0

* Adding semicolons

* Removing vbproj

* Comments

* Updating error message

* Updating error message
  • Loading branch information
issacnitin authored and issacnitinmsft committed Jun 27, 2019
1 parent 53049fa commit 2a4d341
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"loc.messages.dotnetCommandFailed": "Dotnet command failed with non-zero exit code on the following projects : %s",
"loc.messages.noProjectFilesFound": "Project file(s) matching the specified pattern were not found.",
"loc.messages.noPublishFolderFoundToZip": "A publish folder could not be found to zip for project file: %s.",
"loc.messages.noWebProjctFound": "No web project was found in the repository. Web projects are identified by presence of either a web.config file or wwwroot folder in the directory.",
"loc.messages.noWebProjectFound": "No web project was found in the repository. Web projects are identified by presence of either a web.config file, wwwroot folder in the directory, or by the usage of Microsoft.Net.Web.Sdk in your project file. You can set Publish Web Projects property to false (publishWebProjects: false in yml) if your project doesn't follow this convention or if you want to publish projects other than web projects.",
"loc.messages.zipFailed": "Zip failed with error: %s",
"loc.messages.Error_ApiKeyNotSupported": "DotNetCore currently does not support using an encrypted Api Key.",
"loc.messages.Error_ExpectedConfigurationElement": "Invalid xml. Expected element named 'configuration'.",
Expand Down
13 changes: 13 additions & 0 deletions Tasks/DotNetCoreCLIV2/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,19 @@ describe('DotNetCoreExe Suite', function () {
done();
});


it('publish works with publishWebProjects option if .csproj have Microsoft.Net.Sdk.Web', (done: MochaDone) => {
process.env["__projects__"] = "havesdk*/*.csproj;";
process.env["__publishWebProjects__"] = "true";
let tp = path.join(__dirname, 'publishInputs.js')
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);
tr.run();

assert(tr.invokedToolCount == 1, 'should have invoked been invoked once');
assert(tr.succeeded, 'task should have failed');
done();
})

it('publish updates the output with the project name appended', (done: MochaDone) => {
process.env["__projects__"] = "*customoutput/project.json";
process.env["__publishWebProjects__"] = "false";
Expand Down
6 changes: 6 additions & 0 deletions Tasks/DotNetCoreCLIV2/Tests/publishInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"code": 1,
"stdout": "not published",
"stderr": ""
},
"dotnet publish havesdk/project.csproj": {
"code": 0,
"stdout": "published",
"stderr": ""
}
},
"findMatch": {
Expand All @@ -98,6 +103,7 @@ let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"*fail*/project.json": [],
"*customoutput/project.json": ["web3/project.json", "lib2/project.json"],
"dummy/project.json": ["dummy/project.json"],
"havesdk*/*.csproj": ["havesdk/project.csproj"],
"" : []
}
};
Expand Down
39 changes: 34 additions & 5 deletions Tasks/DotNetCoreCLIV2/dotnetcore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import tl = require("azure-pipelines-task-lib/task");
import tr = require("azure-pipelines-task-lib/toolrunner");
import path = require("path");
import fs = require("fs");
import ltx = require("ltx");
var archiver = require('archiver');

import * as packCommand from './packcommand';
Expand Down Expand Up @@ -325,22 +326,50 @@ export class dotNetExe {
}

var projectFiles = utility.getProjectFiles(projectPattern);
var resolvedProjectFiles: string[] = [];

if (searchWebProjects) {
projectFiles = projectFiles.filter(function (file, index, files): boolean {
resolvedProjectFiles = projectFiles.filter(function (file, index, files): boolean {
var directory = path.dirname(file);
return tl.exist(path.join(directory, "web.config"))
|| tl.exist(path.join(directory, "wwwroot"));
});

if (!projectFiles.length) {
tl.error(tl.loc("noWebProjctFound"));
}
if (!resolvedProjectFiles.length) {
var projectFilesUsingWebSdk = projectFiles.filter(this.isWebSdkUsed);
if(!projectFilesUsingWebSdk.length) {
tl.error(tl.loc("noWebProjectFound"));
}
return projectFilesUsingWebSdk;
}
return resolvedProjectFiles;
}

return projectFiles;
}

private isWebSdkUsed(projectfile: string): boolean {
if (projectfile.endsWith('.vbproj')) return false

try {
var fileBuffer: Buffer = fs.readFileSync(projectfile);
var webConfigContent: string;

var fileEncodings = ['utf8', 'utf16le'];

for(var i = 0; i < fileEncodings.length; i++) {
tl.debug("Trying to decode with " + fileEncodings[i]);
webConfigContent = fileBuffer.toString(fileEncodings[i]);
try {
var projectSdkUsed: string = ltx.parse(webConfigContent).getAttr("sdk") || ltx.parse(webConfigContent).getAttr("Sdk");
return projectSdkUsed && projectSdkUsed.toLowerCase() == "microsoft.net.sdk.web";
} catch (error) {}
}
} catch(error) {
tl.warning(error);
}
return false;
}

private isPublishCommand(): boolean {
return this.command === "publish";
}
Expand Down
113 changes: 43 additions & 70 deletions Tasks/DotNetCoreCLIV2/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Tasks/DotNetCoreCLIV2/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@
"dotnetCommandFailed": "Dotnet command failed with non-zero exit code on the following projects : %s",
"noProjectFilesFound": "Project file(s) matching the specified pattern were not found.",
"noPublishFolderFoundToZip": "A publish folder could not be found to zip for project file: %s.",
"noWebProjctFound": "No web project was found in the repository. Web projects are identified by presence of either a web.config file or wwwroot folder in the directory.",
"noWebProjectFound": "No web project was found in the repository. Web projects are identified by presence of either a web.config file, wwwroot folder in the directory, or by the usage of Microsoft.Net.Web.Sdk in your project file. You can set Publish Web Projects property to false (publishWebProjects: false in yml) if your project doesn't follow this convention or if you want to publish projects other than web projects.",
"zipFailed": "Zip failed with error: %s",
"Error_ApiKeyNotSupported": "DotNetCore currently does not support using an encrypted Api Key.",
"Error_ExpectedConfigurationElement": "Invalid xml. Expected element named 'configuration'.",
Expand Down

0 comments on commit 2a4d341

Please sign in to comment.