Welcome to mirror list, hosted at ThFree Co, Russian Federation.

configure.cs « scripts - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 823f88f7cca63e0565b7c529a808c7fd2617fb3b (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.Xml;
using System.Net;
using System.Reflection;

namespace MonoDevelop.Configuration
{
	public class PlatformInfo
	{
		public string AppId;
		public string UpdaterId;
		public string UpdaterVersion;
	}

	public enum Platform
	{
		Mac,
		Windows,
		Linux
	}

	class Program
	{
        static IdeConfigurationTool config;

		static int Main (string[] args)
		{
			try {
                config = new IdeConfigurationTool (new FileInfo(Assembly.GetEntryAssembly().Location).Directory.Parent.FullName);

                if (args.Length == 0)
                {
                    PrintHelp();
                    return 0;
                }

                var cmd = args[0];
                args = args.Skip(1).ToArray();

                switch (cmd)
                {
                    case "get-version":
                        GetVersion(args);
                        break;
                    case "get-releaseid":
                        GetReleaseId(args);
                        break;
                    case "gen-updateinfo":
                        GenerateUpdateInfo(args);
                        break;
                    case "gen-buildinfo":
                        GenerateBuildInfo(args);
                        break;
                    default:
                        Console.WriteLine("Unknown command: " + cmd);
                        return 1;
                }
                return 0;
            }
			catch (UserException ex) {
				Console.WriteLine (ex.Message);
				return 1;
			}
			catch (Exception ex) {
                Console.WriteLine (ex.ToString());
				return 1;
			}
		}

		static void GetVersion (string[] args)
		{
			Console.WriteLine (config.ProductVersion);
		}

		static void GetReleaseId (string[] args)
		{
			Console.WriteLine (config.ReleaseId);
		}

        static void GenerateUpdateInfo(string[] args)
        {
            if (args.Length == 0)
                throw new UserException("Platform config file not provided");
            if (args.Length == 1)
                throw new UserException("Target directory not provided");

            config.GenerateUpdateInfo(args[0], args[1]);
        }

        static void GenerateBuildInfo(string[] args)
        {
            if (args.Length == 0)
                throw new UserException("Target directory not provided");

            config.GenerateBuildInfo(args[0]);
        }

        static void PrintHelp()
        {
            Console.WriteLine("MonoDevelop Configuration Script");
            Console.WriteLine();
            Console.WriteLine("Commands:");
            Console.WriteLine("\tget-version: Prints the version of this release");
            Console.WriteLine("\tget-releaseid: Prints the release id");
            Console.WriteLine("\tgen-updateinfo <config-file> <path>: Generates the updateinfo file");
            Console.WriteLine("\t\tin the provided path");
            Console.WriteLine("\tgen-buildinfo <path>: Generates the buildinfo file in the provided path");
            Console.WriteLine();
        }
    }

    public class IdeConfigurationTool
    {
        public readonly string MonoDevelopPath;
        public readonly string Version;
        public readonly string ProductVersion;
        public readonly string ProductVersionText;
        public readonly string CompatVersion;
        public readonly string AssemblyVersion = "4.0.0.0";
        public readonly string ReleaseId;
        public readonly PlatformInfo PlatformInfo;
       
        public IdeConfigurationTool(string monoDevelopPath)
        {
            MonoDevelopPath = monoDevelopPath;

            string versionTxt = Path.Combine(MonoDevelopPath, "version.config");
            Version = SystemUtil.Grep(versionTxt, @"Version=(.*)");
            ProductVersionText = SystemUtil.Grep(versionTxt, "Label=(.*)");
            CompatVersion = SystemUtil.Grep(versionTxt, "CompatVersion=(.*)");

            Version ver = new Version(Version);
            int vbuild = ver.Build != -1 ? ver.Build : 0;
            var cd = GetVersionCommitDistance(MonoDevelopPath);
            int vbrev = ver.Revision != -1 ? ver.Revision : cd;
            ReleaseId = "" + ver.Major + ver.Minor.ToString("00") + vbuild.ToString("00") + vbrev.ToString("0000");
            ProductVersion = ver.Major + "." + ver.Minor + "." + vbuild + "." + vbrev;
        }

		public void GenerateUpdateInfo (string platformConfigFile, string targetDir)
		{
			PlatformInfo pinfo;
            using (var sr = new StreamReader(platformConfigFile))
            {
				XmlSerializer ser = new XmlSerializer (typeof(PlatformInfo));
				pinfo = (PlatformInfo)ser.Deserialize (sr);
			}

			if (pinfo.AppId != null)
				File.WriteAllText (Path.Combine (targetDir, "updateinfo"), pinfo.AppId + " " + ReleaseId);

			if (pinfo.UpdaterId != null)
				File.WriteAllText (Path.Combine (targetDir, "updateinfo.updater"), pinfo.UpdaterId + " " + pinfo.UpdaterVersion);
		}

        public void GenerateBuildInfo(string targetDir)
		{
            string head = SystemUtil.RunProcess(SystemUtil.GitExe, "rev-parse HEAD", MonoDevelopPath).Trim();

			var txt = "Release ID: " + ReleaseId + "\n";
			txt += "Git revision: " + head + "\n";
			txt += "Build date: " + DateTime.Now.ToString ("yyyy-MM-dd HH:mm:sszz") + "\n";

            File.WriteAllText(Path.Combine(targetDir, "buildinfo"), txt);
		}

		static int GetVersionCommitDistance (string path)
		{
            var blame = new StringReader(SystemUtil.RunProcess(SystemUtil.GitExe, "blame version.config", path));
			string line;
			while ((line = blame.ReadLine ()) != null && line.IndexOf ("Version=") == -1)
				;
			if (line != null) {
				string hash = line.Substring (0, line.IndexOf (' ')).TrimStart ('^');
                string dist = SystemUtil.RunProcess(SystemUtil.GitExe, "rev-list --count " + hash + "..HEAD", path);
				return int.Parse (dist.Trim ());
			}
			return 0;
		}
	}

	class UserException: Exception
	{
		public UserException (string msg)
			: base (msg)
		{
		}
	}

    public static class Logging
    {
        static string logFile;
        static object localLock = new object ();

        public static void Initialize(string logFileName)
        {
            logFile = logFileName;
        }

        public static void ReportError(string msg)
        {
            if (logFile != null) {
                lock (localLock)
                    File.AppendAllText(logFile, "[ERROR] " + msg + Environment.NewLine);
            }
            Console.WriteLine("[ERROR] " + msg);
        }

        public static void ReportInfo(string msg)
        {
            if (logFile != null) {
                lock (localLock)
                    File.AppendAllText(logFile, "[INFO] " + msg + Environment.NewLine);
            }
            Console.WriteLine(msg);
        }

        public static void ReportDebug(string msg)
        {
            if (logFile != null)
            {
                lock (localLock)
                    File.AppendAllText(logFile, "[INFO] " + msg + Environment.NewLine);
            }
        }
    }

	public static class SystemUtil
	{
		static SystemUtil ()
		{
			if (Path.DirectorySeparatorChar == '\\')
				Platform = Platform.Windows;
			else if (Directory.Exists ("/Library/Application Support"))
				Platform = Platform.Mac;
			else
				Platform = Platform.Linux;

			if (Platform == Platform.Windows) {
				if (File.Exists (@"c:\Program Files\Git\bin\git.exe"))
					GitExe = @"c:\Program Files\Git\bin\git.exe";
				else if (File.Exists (@"c:\Program Files (x86)\Git\bin\git.exe"))
					GitExe = @"c:\Program Files (x86)\Git\bin\git.exe";
				else if (File.Exists (@"c:\msysgit\bin\git.exe"))
					GitExe = @"c:\msysgit\bin\git.exe";
				else
					GitExe = "git.exe";
			} else {
				if (File.Exists ("/usr/bin/git"))
					GitExe = "/usr/bin/git";
				else
					GitExe = "git";
			}
		}

		public static string GitExe { get; private set; }

		public static Platform Platform { get; private set; }

        public static string Grep(string file, string regex)
        {
            string txt = File.ReadAllText(file);
            var m = Regex.Match(txt, regex);
            if (m == null)
                throw new UserException("Match not found for regex: " + regex);
            if (m.Groups.Count != 2)
                throw new UserException("Invalid regex: expression must have a single capture group: " + regex);
            Group cap = m.Groups[1];
            return cap.Value;
        }

       public static string RunProcess(string file, string args, string workingDir)
       {
           Logging.ReportDebug(file + " " + args);

            Process p = new Process();
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.WorkingDirectory = workingDir;
            p.StartInfo.FileName = file;
            p.StartInfo.Arguments = args;
            StringBuilder sb = new StringBuilder();
            p.OutputDataReceived += delegate(object s, DataReceivedEventArgs e)
            {
                Logging.ReportDebug(e.Data);
                sb.AppendLine(e.Data);
            };
            p.ErrorDataReceived += delegate(object s, DataReceivedEventArgs e)
            {
                Logging.ReportDebug(e.Data);
            };
            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
            p.WaitForExit();
            if (p.ExitCode != 0)
                throw new UserException(file + " failed");
            return sb.ToString();
        }
    }
}