Skip to content

Commit b9e1127

Browse files
JinhuafeiHongGit
authored andcommitted
add support for non web proj (#32)
* add support for non web proj * fix rebuild issue and update Roslyn version * fix rebuild issue, update Roslyn version and add appsetting for Roslyn compiler location
1 parent 30f97fb commit b9e1127

9 files changed

Lines changed: 232 additions & 17 deletions

File tree

src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/AppSettings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ private static void LoadSettings(NameValueCollection appSettings) {
2020
if (!bool.TryParse(disableProfilingDuringCompilation, out _disableProfilingDuringCompilation)) {
2121
_disableProfilingDuringCompilation = true;
2222
}
23+
24+
_roslynCompilerLocation = appSettings["aspnet:RoslynCompilerLocation"];
2325
}
2426

2527
private static void EnsureSettingsLoaded() {
@@ -46,5 +48,13 @@ public static bool DisableProfilingDuringCompilation {
4648
return _disableProfilingDuringCompilation;
4749
}
4850
}
51+
52+
private static string _roslynCompilerLocation = string.Empty;
53+
public static string RoslynCompilerLocation {
54+
get {
55+
EnsureSettingsLoaded();
56+
return _roslynCompilerLocation;
57+
}
58+
}
4959
}
5060
}

src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/Util/CompilationSettings.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,14 @@ static CompilationSettingsHelper() {
6363
}
6464
}
6565

66+
// locate Roslyn compilers order: 1. environment variable 2. appsetting 3. default location
6667
var customRoslynCompilerLocation = Environment.GetEnvironmentVariable(CustomRoslynCompilerLocation, EnvironmentVariableTarget.Process);
67-
if(customRoslynCompilerLocation != null)
68+
if(string.IsNullOrEmpty(customRoslynCompilerLocation))
69+
{
70+
customRoslynCompilerLocation = AppSettings.RoslynCompilerLocation;
71+
}
72+
73+
if(!string.IsNullOrEmpty(customRoslynCompilerLocation))
6874
{
6975
_csc = new CompilerSettings($"{customRoslynCompilerLocation}\\csc.exe", ttl);
7076
_vb = new CompilerSettings($"{customRoslynCompilerLocation}\\vbc.exe", ttl);

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/build/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616
</ItemGroup>
1717
</Target>
1818
<Target Name="CopyRoslynCompilerFilesToOutputDirectory" AfterTargets="CopyFilesToOutputDirectory">
19-
<Copy SourceFiles="@(RoslyCompilerFiles)" DestinationFolder="$(WebProjectOutputDir)\bin\roslyn" ContinueOnError="true" SkipUnchangedFiles="true" Retries="0" />
19+
<PropertyGroup>
20+
<RoslynToolsDestinationFolder>$(WebProjectOutputDir)\bin\roslyn</RoslynToolsDestinationFolder>
21+
<RoslynToolsDestinationFolder Condition=" '$(WebProjectOutputDir)' == '' ">$(OutputPath)\roslyn</RoslynToolsDestinationFolder>
22+
</PropertyGroup>
23+
<Copy SourceFiles="@(RoslyCompilerFiles)" DestinationFolder="$(RoslynToolsDestinationFolder)" ContinueOnError="true" SkipUnchangedFiles="true" Retries="0" />
2024
<ItemGroup Condition="'$(MSBuildLastTaskResult)' == 'True'" >
21-
<FileWrites Include="$(WebProjectOutputDir)\bin\roslyn\*" />
25+
<FileWrites Include="$(RoslynToolsDestinationFolder)\*" />
2226
</ItemGroup>
2327
</Target>
2428
<Target Name = "KillVBCSCompilerAndRetryCopy" AfterTargets="CopyRoslynCompilerFilesToOutputDirectory" Condition="'$(MSBuildLastTaskResult)' == 'False'" >
25-
<KillProcess ProcessName="VBCSCompiler" ImagePath="$(WebProjectOutputDir)" />
26-
<Copy SourceFiles="@(RoslyCompilerFiles)" DestinationFolder="$(WebProjectOutputDir)\bin\roslyn" ContinueOnError="true" SkipUnchangedFiles="true" />
29+
<KillProcess ProcessName="VBCSCompiler" ImagePath="$(RoslynToolsDestinationFolder)" />
30+
<Copy SourceFiles="@(RoslyCompilerFiles)" DestinationFolder="$(RoslynToolsDestinationFolder)" ContinueOnError="true" SkipUnchangedFiles="true" />
2731
<ItemGroup>
28-
<FileWrites Include="$(WebProjectOutputDir)\bin\roslyn\*" />
32+
<FileWrites Include="$(RoslynToolsDestinationFolder)\*" />
2933
</ItemGroup>
3034
</Target>
3135
<UsingTask TaskName="KillProcess" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
3+
<appSettings xdt:Transform="InsertIfMissing">
4+
</appSettings>
5+
6+
<appSettings>
7+
<add key="aspnet:RoslynCompilerLocation" xdt:Transform="Remove" xdt:Locator="Match(key)" />
8+
<add key="aspnet:RoslynCompilerLocation" value="roslyn" xdt:Transform="Insert" />
9+
</appSettings>
10+
11+
<!-- If system.codedom tag is absent -->
12+
<system.codedom xdt:Transform="InsertIfMissing">
13+
</system.codedom>
14+
15+
<!-- If compilers tag is absent -->
16+
<system.codedom>
17+
<compilers xdt:Transform="InsertIfMissing">
18+
</compilers>
19+
</system.codedom>
20+
21+
<!-- If a .cs compiler is already present, the existing entry needs to be removed before inserting the new entry -->
22+
<system.codedom>
23+
<compilers>
24+
<compiler
25+
extension=".cs"
26+
xdt:Transform="Remove"
27+
xdt:Locator="Match(extension)" />
28+
</compilers>
29+
</system.codedom>
30+
31+
<!-- Inserting the new compiler -->
32+
<system.codedom>
33+
<compilers>
34+
<compiler
35+
language="c#;cs;csharp"
36+
extension=".cs"
37+
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
38+
warningLevel="4"
39+
compilerOptions="/langversion:6 /nowarn:1659;1699;1701"
40+
xdt:Transform="Insert" />
41+
</compilers>
42+
</system.codedom>
43+
44+
<!-- If a .vb compiler is already present, the existing entry needs to be removed before inserting the new entry -->
45+
<system.codedom>
46+
<compilers>
47+
<compiler
48+
extension=".vb"
49+
xdt:Transform="Remove"
50+
xdt:Locator="Match(extension)" />
51+
</compilers>
52+
</system.codedom>
53+
54+
<!-- Inserting the new compiler -->
55+
<system.codedom>
56+
<compilers>
57+
<compiler
58+
language="vb;vbs;visualbasic;vbscript"
59+
extension=".vb"
60+
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
61+
warningLevel="4"
62+
compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
63+
xdt:Transform="Insert" />
64+
</compilers>
65+
</system.codedom>
66+
</configuration>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
3+
<appSettings>
4+
<add key="aspnet:RoslynCompilerLocation" value="roslyn" xdt:Transform="Remove" xdt:Locator="Match(key)" />
5+
</appSettings>
6+
<appSettings xdt:Transform="Remove" xdt:Locator="Condition(count(child::*) = 0)">
7+
</appSettings>
8+
9+
<system.codedom>
10+
<compilers>
11+
<compiler
12+
extension=".cs"
13+
xdt:Transform="Remove"
14+
xdt:Locator="Match(extension)" />
15+
</compilers>
16+
</system.codedom>
17+
<system.codedom>
18+
<compilers>
19+
<compiler
20+
extension=".vb"
21+
xdt:Transform="Remove"
22+
xdt:Locator="Match(extension)" />
23+
</compilers>
24+
</system.codedom>
25+
<system.codedom>
26+
<compilers xdt:Transform="Remove" xdt:Locator="Condition(count(child::*) = 0)" />
27+
</system.codedom>
28+
<system.codedom xdt:Transform="Remove" xdt:Locator="Condition(count(child::*) = 0)" />
29+
</configuration>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
3+
<appSettings xdt:Transform="InsertIfMissing">
4+
</appSettings>
5+
6+
<appSettings>
7+
<add key="aspnet:RoslynCompilerLocation" xdt:Transform="Remove" xdt:Locator="Match(key)" />
8+
<add key="aspnet:RoslynCompilerLocation" value="roslyn" xdt:Transform="Insert" />
9+
</appSettings>
10+
11+
<!-- If system.codedom tag is absent -->
12+
<system.codedom xdt:Transform="InsertIfMissing">
13+
</system.codedom>
14+
15+
<!-- If compilers tag is absent -->
16+
<system.codedom>
17+
<compilers xdt:Transform="InsertIfMissing">
18+
</compilers>
19+
</system.codedom>
20+
21+
<!-- If a .cs compiler is already present, the existing entry needs to be removed before inserting the new entry -->
22+
<system.codedom>
23+
<compilers>
24+
<compiler
25+
extension=".cs"
26+
xdt:Transform="Remove"
27+
xdt:Locator="Match(extension)" />
28+
</compilers>
29+
</system.codedom>
30+
31+
<!-- Inserting the new compiler -->
32+
<system.codedom>
33+
<compilers>
34+
<compiler
35+
language="c#;cs;csharp"
36+
extension=".cs"
37+
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
38+
warningLevel="4"
39+
compilerOptions="/langversion:default /nowarn:1659;1699;1701"
40+
xdt:Transform="Insert" />
41+
</compilers>
42+
</system.codedom>
43+
44+
<!-- If a .vb compiler is already present, the existing entry needs to be removed before inserting the new entry -->
45+
<system.codedom>
46+
<compilers>
47+
<compiler
48+
extension=".vb"
49+
xdt:Transform="Remove"
50+
xdt:Locator="Match(extension)" />
51+
</compilers>
52+
</system.codedom>
53+
54+
<!-- Inserting the new compiler -->
55+
<system.codedom>
56+
<compilers>
57+
<compiler
58+
language="vb;vbs;visualbasic;vbscript"
59+
extension=".vb"
60+
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
61+
warningLevel="4"
62+
compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
63+
xdt:Transform="Insert" />
64+
</compilers>
65+
</system.codedom>
66+
</configuration>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
3+
<appSettings>
4+
<add key="aspnet:RoslynCompilerLocation" value="roslyn" xdt:Transform="Remove" xdt:Locator="Match(key)" />
5+
</appSettings>
6+
<appSettings xdt:Transform="Remove" xdt:Locator="Condition(count(child::*) = 0)">
7+
</appSettings>
8+
9+
<system.codedom>
10+
<compilers>
11+
<compiler
12+
extension=".cs"
13+
xdt:Transform="Remove"
14+
xdt:Locator="Match(extension)" />
15+
</compilers>
16+
</system.codedom>
17+
<system.codedom>
18+
<compilers>
19+
<compiler
20+
extension=".vb"
21+
xdt:Transform="Remove"
22+
xdt:Locator="Match(extension)" />
23+
</compilers>
24+
</system.codedom>
25+
<system.codedom>
26+
<compilers xdt:Transform="Remove" xdt:Locator="Condition(count(child::*) = 0)" />
27+
</system.codedom>
28+
<system.codedom xdt:Transform="Remove" xdt:Locator="Condition(count(child::*) = 0)" />
29+
</configuration>

tools/RoslynCodeProvider.Extensions.targets

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,54 @@
3030
3131
var targetFilePath = Path.Combine(Path.GetTempPath(), string.Format(roslynPackageName, ReferenceRoslynNupkgVersion));
3232
var targetExtractPath = Path.Combine(Path.GetTempPath(), LocalRoslynFolderName);
33-
var targetPath = Path.Combine(NupkgToolPath, LocalRoslynFolderName);
33+
var packageToolsPath = Path.Combine(NupkgToolPath, LocalRoslynFolderName);
3434
if (Directory.Exists(targetExtractPath))
3535
{
3636
Directory.Delete(targetExtractPath, true);
3737
}
38-
if (Directory.Exists(targetPath))
38+
if (Directory.Exists(packageToolsPath))
3939
{
40-
Directory.Delete(targetPath, true);
40+
Directory.Delete(packageToolsPath, true);
4141
}
4242
4343
wc.DownloadFile(string.Format(roslynNugetBaseUri, ReferenceRoslynNupkgVersion), targetFilePath);
44+
Log.LogMessage("Microsoft.Net.Compilers.{0}.nupkg is downloaded", ReferenceRoslynNupkgVersion);
45+
4446
ZipFile.ExtractToDirectory(targetFilePath, targetExtractPath);
45-
Directory.CreateDirectory(targetPath);
47+
Directory.CreateDirectory(packageToolsPath);
4648
foreach(var file in Directory.GetFiles(Path.Combine(targetExtractPath, "tools"))){
4749
var fi = new FileInfo(file);
48-
File.Copy(file, Path.Combine(targetPath, fi.Name));
50+
File.Copy(file, Path.Combine(packageToolsPath, fi.Name));
4951
}
5052
5153
targetFilePath = Path.Combine(Path.GetTempPath(), string.Format(roslynPackageName, ReferenceLatestRoslynNupkgVersion));
5254
targetExtractPath = Path.Combine(Path.GetTempPath(), LocalLatestRoslynFolderName);
53-
targetPath = Path.Combine(NupkgToolPath, LocalLatestRoslynFolderName);
55+
packageToolsPath = Path.Combine(NupkgToolPath, LocalLatestRoslynFolderName);
5456
if (Directory.Exists(targetExtractPath))
5557
{
5658
Directory.Delete(targetExtractPath, true);
5759
}
58-
if (Directory.Exists(targetPath))
60+
if (Directory.Exists(packageToolsPath))
5961
{
60-
Directory.Delete(targetPath, true);
62+
Directory.Delete(packageToolsPath, true);
6163
}
6264
6365
wc.DownloadFile(string.Format(roslynNugetBaseUri, ReferenceLatestRoslynNupkgVersion), targetFilePath);
66+
Log.LogMessage("Microsoft.Net.Compilers.{0}.nupkg is downloaded", ReferenceLatestRoslynNupkgVersion);
67+
6468
ZipFile.ExtractToDirectory(targetFilePath, targetExtractPath);
65-
Directory.CreateDirectory(targetPath);
69+
Directory.CreateDirectory(packageToolsPath);
6670
foreach (var file in Directory.GetFiles(Path.Combine(targetExtractPath, "tools")))
6771
{
6872
var fi = new FileInfo(file);
69-
File.Copy(file, Path.Combine(targetPath, fi.Name));
73+
File.Copy(file, Path.Combine(packageToolsPath, fi.Name));
7074
}
7175
}
7276
}
7377
catch (Exception ex)
7478
{
7579
Log.LogErrorFromException(ex);
80+
return false;
7681
}
7782
return true;
7883
]]>

tools/RoslynCodeProvider.settings.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<PropertyGroup Label="NuGet package dependencies">
1515
<MSNetCompilersNuGetPackageVersion>1.3.2</MSNetCompilersNuGetPackageVersion>
16-
<MSNetCompilersNuGetPackageLatestVersion>2.4.0</MSNetCompilersNuGetPackageLatestVersion>
16+
<MSNetCompilersNuGetPackageLatestVersion>2.6.1</MSNetCompilersNuGetPackageLatestVersion>
1717
</PropertyGroup>
1818

1919
<!-- Default properties -->

0 commit comments

Comments
 (0)