Skip to content

Commit

Permalink
Build: Refactor script file generation
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinCampbell committed Jul 23, 2017
1 parent 70f3b72 commit 84d788d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 42 deletions.
125 changes: 83 additions & 42 deletions scripts/artifacts.cake
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");
}
6 changes: 6 additions & 0 deletions scripts/common.cake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public static class FileHelper
Log.Debug(Verbosity.Diagnostic, "Delete file: {0}.", path);
System.IO.File.Delete(path);
}

public static bool Exists(string path) =>
System.IO.File.Exists(path);

public static void WriteAllLines(string path, string[] contents) =>
System.IO.File.WriteAllLines(path, contents);
}

public static class DirectoryHelper
Expand Down

0 comments on commit 84d788d

Please sign in to comment.