forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
129 lines (112 loc) · 3.27 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using CommandLine;
namespace DocGenerator
{
public static class Program
{
static Program()
{
string P(string path)
{
return path.Replace(@"\", Path.DirectorySeparatorChar.ToString());
}
var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
var globalJson = P(@"..\..\..\global.json");
if (currentDirectory.Name == "DocGenerator" && currentDirectory.Parent.Name == "CodeGeneration")
{
Console.WriteLine("IDE: " + currentDirectory);
InputDirPath = P(@"..\..\");
OutputDirPath = P(@"..\..\..\docs");
BuildOutputPath = P(@"..\..\..\src");
}
else
{
globalJson = P(@"..\..\..\..\global.json");
Console.WriteLine("CMD: " + currentDirectory);
InputDirPath = P(@"..\..\..\..\src");
OutputDirPath = P(@"..\..\..\..\docs");
BuildOutputPath = P(@"..\..\..\..\build\output");
}
var globalJsonVersion = string.Join(".", Regex.Matches(File.ReadAllText(globalJson), "\"version\": \"(.*)\"")
.Last().Groups
.Last().Value
.Split(".")
.Take(2));
DocVersion = globalJsonVersion;
var process = new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = "git.exe",
CreateNoWindow = true,
WorkingDirectory = Environment.CurrentDirectory,
Arguments = "rev-parse --abbrev-ref HEAD"
}
};
try
{
process.Start();
BranchName = process.StandardOutput.ReadToEnd().Trim();
process.WaitForExit();
}
catch (Exception)
{
BranchName = "master";
}
finally
{
process.Dispose();
}
}
/// <summary>
/// The branch name to include in generated docs to link back to the original source file
/// </summary>
public static string BranchName { get; set; }
public static string BuildOutputPath { get; }
/// <summary>
/// The Elasticsearch documentation version to link to
/// </summary>
public static string DocVersion { get; set; }
public static string InputDirPath { get; }
public static string OutputDirPath { get; }
private static int Main(string[] args)
{
return Parser.Default.ParseArguments<DocGeneratorOptions>(args)
.MapResult(
opts =>
{
try
{
if (!string.IsNullOrEmpty(opts.BranchName))
BranchName = opts.BranchName;
if (!string.IsNullOrEmpty(opts.DocVersion))
DocVersion = opts.DocVersion;
Console.WriteLine($"Using branch name {BranchName} in documentation");
Console.WriteLine($"Using doc reference version {DocVersion} in documentation");
LitUp.GoAsync(args).Wait();
return 0;
}
catch (AggregateException ae)
{
var ex = ae.InnerException ?? ae;
Console.WriteLine(ex.Message);
return 1;
}
},
errs => 1);
}
}
public class DocGeneratorOptions
{
[Option('b', "branch", Required = false, HelpText = "The version that appears in generated from source link")]
public string BranchName { get; set; }
[Option('d', "docs", Required = false, HelpText = "The version that links to the Elasticsearch reference documentation")]
public string DocVersion { get; set; }
}
}