-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add RoslynCodeTaskFactory and DownloadFile samples #3313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| LICENSE |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using System; | ||
|
|
||
| namespace DownloadFile | ||
| { | ||
| public class Class1 | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netcoreapp2.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
|
|
||
| <!-- | ||
| The DownloadFile task was added in 15.8.59, previous versions of MSBuild will just fail so don't import the targets | ||
| --> | ||
| <Import Project="DownloadFile.targets" Condition=" '$(MSBuildVersion)' != '' And '$(MSBuildVersion)' >= '15.8.59' " /> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <Target Name="DownloadLicense" | ||
| AfterTargets="PrepareForBuild"> | ||
|
|
||
| <!-- Download a file --> | ||
| <DownloadFile | ||
| SourceUrl="https://raw.githubusercontent.com/Microsoft/msbuild/master/LICENSE" | ||
| DestinationFolder="$(MSBuildProjectDirectory)"> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you specify a path or something so this can be named |
||
| <Output TaskParameter="DownloadedFile" ItemName="Content" /> | ||
| </DownloadFile> | ||
|
|
||
| <!-- Include the f--> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the f? |
||
| <ItemGroup> | ||
| <Content Include="LICENSE" CopyToOutputDirectory="PreserveNewest" /> | ||
| </ItemGroup> | ||
| </Target> | ||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using System; | ||
|
|
||
| namespace RoslynCodeTaskFactory | ||
| { | ||
| public class Class1 | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <!-- | ||
| RoslynCodeTaskFactory was added in 15.8.59, previous versions of MSBuild will just fail so don't import the targets | ||
| --> | ||
| <Import Project="RoslynCodeTaskFactory.targets" Condition=" '$(MSBuildVersion)' != '' And '$(MSBuildVersion)' >= '15.8.59' " /> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
|
|
||
| <!-- A simple in-line task that logs some messages and returns a string --> | ||
| <UsingTask TaskName="MySample" | ||
| TaskFactory="RoslynCodeTaskFactory" | ||
| AssemblyFile="$(MSBuildBinPath)\Microsoft.Build.Tasks.Core.dll"> | ||
| <ParameterGroup> | ||
| <Parameter1 ParameterType="System.String" Required="true" /> | ||
| <Parameter2 ParameterType="System.String" /> | ||
| <Parameter3 ParameterType="System.String" Output="true" /> | ||
|
|
||
| </ParameterGroup> | ||
| <Task> | ||
| <Using Namespace="System" /> | ||
| <Code Type="Fragment" Language="C#"> | ||
| <![CDATA[ | ||
| Log.LogMessage(MessageImportance.High, "Hello from an inline task created by Roslyn!"); | ||
| Log.LogMessageFromText($"Parameter1: '{Parameter1}'", MessageImportance.High); | ||
| Log.LogMessageFromText($"Parameter2: '{Parameter2}'", MessageImportance.High); | ||
| Parameter3 = "A value from the Roslyn CodeTaskFactory"; | ||
| ]]> | ||
| </Code> | ||
| </Task> | ||
| </UsingTask> | ||
|
|
||
| <!-- A simple in-line task that combines paths --> | ||
| <UsingTask TaskName="PathCombine" | ||
| TaskFactory="RoslynCodeTaskFactory" | ||
| AssemblyFile="$(MSBuildBinPath)\Microsoft.Build.Tasks.Core.dll"> | ||
| <ParameterGroup> | ||
| <Paths ParameterType="System.String[]" Required="true" /> | ||
| <Combined ParameterType="System.String" Output="true" /> | ||
| </ParameterGroup> | ||
| <Task> | ||
| <Using Namespace="System" /> | ||
| <Code Type="Fragment" Language="C#"> | ||
| <![CDATA[ | ||
| Combined = Path.Combine(Paths); | ||
| ]]> | ||
| </Code> | ||
| </Task> | ||
| </UsingTask> | ||
|
|
||
| <!-- A simple in-line task that gets the file name of a path --> | ||
| <UsingTask TaskName="PathGetFileName" | ||
| TaskFactory="RoslynCodeTaskFactory" | ||
| AssemblyFile="$(MSBuildBinPath)\Microsoft.Build.Tasks.Core.dll"> | ||
| <ParameterGroup> | ||
| <Path ParameterType="System.String" Required="true" /> | ||
| <FileName ParameterType="System.String" Output="true" /> | ||
| </ParameterGroup> | ||
| <Task> | ||
|
|
||
| <Using Namespace="System" /> | ||
| <Code Type="Fragment" Language="C#"> | ||
| <![CDATA[ | ||
| FileName = System.IO.Path.GetFileName(Path); | ||
| ]]> | ||
| </Code> | ||
| </Task> | ||
| </UsingTask> | ||
|
|
||
| <!-- A simple in-line class task which inherits parameters from its base class --> | ||
| <UsingTask TaskName="InterceptingCopy" | ||
| TaskFactory="RoslynCodeTaskFactory" | ||
| AssemblyFile="$(MSBuildBinPath)\Microsoft.Build.Tasks.Core.dll"> | ||
| <Task> | ||
| <Reference Include="Microsoft.Build.Tasks.Core"/> | ||
| <Code Type="Class" Language="cs"> | ||
| <![CDATA[ | ||
| using Microsoft.Build.Framework; | ||
| using System; | ||
|
|
||
| public class InterceptingCopy : Microsoft.Build.Tasks.Copy | ||
| { | ||
| public override bool Execute() | ||
| { | ||
| Log.LogMessage(MessageImportance.High, "Copying '{0}' to '{1}'", String.Join(", ", (object[])SourceFiles), String.Join(", ", (object[])DestinationFiles)); | ||
| return base.Execute(); | ||
| } | ||
| } | ||
| ]]> | ||
| </Code> | ||
| </Task> | ||
| </UsingTask> | ||
|
|
||
| <Target Name="RunSampleTask" AfterTargets="Build"> | ||
| <!-- Call the compiled task --> | ||
| <MySample Parameter1="A value for parameter 1" Parameter2="A value for parameter 2"> | ||
| <Output TaskParameter="Parameter3" PropertyName="NewProperty" /> | ||
| </MySample> | ||
|
|
||
| <!-- Log whatever text is in NewProperty that came from Parameter3 --> | ||
| <Message Text="NewProperty: '$(NewProperty)'" Importance="High" /> | ||
|
|
||
| <PathCombine Paths="$(Temp);MyFolder;$([System.Guid]::NewGuid()).txt"> | ||
| <Output TaskParameter="Combined" PropertyName="MyCombinedPaths" /> | ||
| </PathCombine> | ||
|
|
||
| <Message Text="Combined Paths: '$(MyCombinedPaths)'" Importance="High" /> | ||
|
|
||
| <PathGetFileName Path="$(MyCombinedPaths)"> | ||
| <Output TaskParameter="FileName" PropertyName="MyFileName" /> | ||
| </PathGetFileName> | ||
|
|
||
| <Message Text="File name: '$(MyFileName)'" Importance="High" /> | ||
|
|
||
| <InterceptingCopy SourceFiles="Class1.cs" DestinationFiles="$(OutputPath)CopyOfClass1.cs" /> | ||
| </Target> | ||
|
|
||
| </Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say to not specify the xml header or the namespace in samples, since they're no longer needed.