-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build: Refactor script file generation
- Loading branch information
1 parent
70f3b72
commit 84d788d
Showing
2 changed files
with
89 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,57 +1,98 @@ | ||
#load "common.cake" | ||
#load "runhelpers.cake" | ||
|
||
using System.Collections.Generic; | ||
|
||
/// <summary> | ||
/// Generate the scripts which target the OmniSharp binaries. | ||
/// </summary> | ||
/// <param name="outputRoot">The root folder where the publised (or installed) binaries are located</param> | ||
void CreateRunScript(string outputRoot, string scriptFolder) | ||
{ | ||
CreateScript(outputRoot, scriptFolder, "net46"); | ||
CreateScript(outputRoot, scriptFolder, "netcoreapp1.1"); | ||
} | ||
|
||
private void CreateScript(string outputRoot, string scriptFolder, string framework) | ||
{ | ||
var scriptPath = GetScriptPath(scriptFolder, framework); | ||
var omniSharpPath = GetOmniSharpPath(outputRoot, framework); | ||
var content = GetScriptContent(omniSharpPath, framework); | ||
|
||
if (FileHelper.Exists(scriptPath)) | ||
{ | ||
FileHelper.Delete(scriptPath); | ||
} | ||
|
||
FileHelper.WriteAllLines(scriptPath, content); | ||
|
||
if (!IsRunningOnWindows()) | ||
{ | ||
Run("chmod", $"+x \"{scriptPath}\""); | ||
} | ||
} | ||
|
||
private string GetScriptPath(string scriptFolder, string framework) | ||
{ | ||
var result = CombinePaths(scriptFolder, "OmniSharp"); | ||
|
||
if (IsCore(framework)) | ||
{ | ||
result += ".Core"; | ||
} | ||
|
||
if (IsRunningOnWindows()) | ||
{ | ||
var desktopScript = System.IO.Path.Combine(scriptFolder, "OmniSharp.cmd"); | ||
var coreScript = System.IO.Path.Combine(scriptFolder, "OmniSharp.Core.cmd"); | ||
var omniSharpPath = System.IO.Path.Combine(System.IO.Path.GetFullPath(outputRoot), "{0}", "OmniSharp"); | ||
var content = new string[] { | ||
"SETLOCAL", | ||
"", | ||
$"\"{omniSharpPath}\" %*" | ||
}; | ||
if (System.IO.File.Exists(desktopScript)) | ||
{ | ||
System.IO.File.Delete(desktopScript); | ||
} | ||
content[2] = String.Format(content[2], "net46"); | ||
System.IO.File.WriteAllLines(desktopScript, content); | ||
if (System.IO.File.Exists(coreScript)) | ||
{ | ||
System.IO.File.Delete(coreScript); | ||
} | ||
content[2] = String.Format(content[2], "netcoreapp1.1"); | ||
System.IO.File.WriteAllLines(coreScript, content); | ||
result += ".cmd"; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private string GetOmniSharpPath(string outputRoot, string framework) | ||
{ | ||
var result = CombinePaths(PathHelper.GetFullPath(outputRoot), framework, "OmniSharp"); | ||
|
||
if (!IsCore(framework)) | ||
{ | ||
result += ".exe"; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private string[] GetScriptContent(string omniSharpPath, string framework) | ||
{ | ||
var lines = new List<string>(); | ||
|
||
if (IsRunningOnWindows()) | ||
{ | ||
lines.Add("SETLOCAL"); | ||
} | ||
else | ||
{ | ||
var desktopScript = System.IO.Path.Combine(scriptFolder, "OmniSharp"); | ||
var coreScript = System.IO.Path.Combine(scriptFolder, "OmniSharp.Core"); | ||
var omniSharpPath = System.IO.Path.Combine(System.IO.Path.GetFullPath(outputRoot), "{1}", "OmniSharp"); | ||
var content = new string[] { | ||
"#!/bin/bash", | ||
"", | ||
$"{{0}} \"{omniSharpPath}{{2}}\" \"$@\"" | ||
}; | ||
if (System.IO.File.Exists(desktopScript)) | ||
{ | ||
System.IO.File.Delete(desktopScript); | ||
} | ||
content[2] = String.Format(content[2], "mono --assembly-loader=strict", "net46", ".exe"); | ||
System.IO.File.WriteAllLines(desktopScript, content); | ||
Run("chmod", $"+x \"{desktopScript}\""); | ||
if (System.IO.File.Exists(coreScript)) | ||
{ | ||
System.IO.File.Delete(coreScript); | ||
} | ||
content[2] = String.Format(content[2], "", "netcoreapp1.1", ""); | ||
System.IO.File.WriteAllLines(coreScript, content); | ||
Run("chmod", $"+x \"{desktopScript}\""); | ||
lines.Add("#!/bin/bash"); | ||
} | ||
|
||
lines.Add(""); | ||
|
||
var arguments = IsRunningOnWindows() | ||
? "%*" | ||
: "\"$@\""; | ||
|
||
if (IsCore(framework) || IsRunningOnWindows()) | ||
{ | ||
lines.Add($"\"{omniSharpPath}\" {arguments}"); | ||
} | ||
else // !isCore && !IsRunningOnWindows, i.e. Mono | ||
{ | ||
lines.Add($"mono --assembly-loader=strict \"{omniSharpPath}\" {arguments}"); | ||
} | ||
|
||
return lines.ToArray(); | ||
} | ||
|
||
private bool IsCore(string framework) | ||
{ | ||
return framework.StartsWith("netcoreapp"); | ||
} |
This file contains 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