Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Linq;
- using System.Collections.Generic;
- using System.Drawing;
- using Console = Colorful.Console;
- using Figlet = Colorful.Figlet;
- using FigletFont = Colorful.FigletFont;
- namespace ParticleBuild
- {
- class Program
- {
- public static void Main(string[] arg)
- {
- FigletFont font = FigletFont.Load(@"D:\Shahadat\ParticleBuild\ParticleBuild\bin\Debug\netcoreapp3.1\assets\colossal.flf");
- Figlet figlet = new Figlet(font);
- Console.WriteLine(figlet.ToAscii("Build Success"), ColorTranslator.FromHtml("#90ee90"));
- var workStationRelativePath = @"D:\Shahadat\Workstation";
- var particleRelativePath = GetParticleRelativePathFromWorkStation(workStationRelativePath);
- bool ok = CheckGitStatus(particleRelativePath);
- if(ok == false) {
- Console.WriteLine("Commit your code or Stash them", Color.Red);
- return;
- }
- ok = CheckGitPull(particleRelativePath);
- if(ok == false) {
- Console.WriteLine("Resolve Merge Conflict", Color.Red);
- return;
- }
- using (Process myProcess = new Process())
- {
- myProcess.StartInfo.FileName = "CMD.exe";
- myProcess.StartInfo.Arguments = "/C git status";
- myProcess.StartInfo.WorkingDirectory = GetParticleRelativePathFromWorkStation(workStationRelativePath);
- myProcess.StartInfo.RedirectStandardOutput = true;
- myProcess.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
- myProcess.Start();
- myProcess.BeginOutputReadLine();
- //StreamReader reader = myProcess.StandardOutput.ReadLine();
- myProcess.WaitForExit();
- }
- Console.WriteLine("Version changing...");
- var newVersionNo = ChangeVersionNumber(workStationRelativePath);
- if (newVersionNo.Length <= 20)
- {
- Console.WriteLine("Error occured in version changing");
- return;
- }
- Console.WriteLine("New version: " + newVersionNo.Trim());
- GitPushAndCommit(particleRelativePath);
- //Console.WriteLine("Production build running...");
- //using (Process myProcess = new Process())
- //{
- // myProcess.StartInfo.FileName = "CMD.exe";
- // myProcess.StartInfo.Arguments = "/C cd " + GetParticleRelativePathFromWorkStation(workStationRelativePath) + "/apps/particle && " + "npm run bmax";
- // myProcess.Start();
- // // This code assumes the process you are starting will terminate itself.
- // // Given that is is started without a window so you cannot terminate it
- // // on the desktop, it must terminate itself or you can do it programmatically
- // // from this application using the Kill method.
- // myProcess.WaitForExit();
- //}
- // string sourceDir = @"D:\Shahadat\Workstation\orbitax-particle_fork-shahadat\apps\particle\projects\particle\src\lib\assets";
- // string destDir = @"D:\Shahadat\Workstation\orbitax-particle_fork-shahadat\apps\particle\dist\particle\lib\assets";
- // CopyDirectories(sourceDir, destDir);
- //Console.WriteLine("Finished Copy");
- //Console.WriteLine("Now publish him");
- //using (Process myProcess = new Process())
- //{
- // myProcess.StartInfo.WorkingDirectory = @"D:\Shahadat\Workstation\orbitax-particle_fork-shahadat\apps\particle\dist\particle";
- // myProcess.StartInfo.FileName = "CMD.exe";
- // myProcess.StartInfo.Arguments = "/C npm install";
- // myProcess.Start();
- // // This code assumes the process you are starting will terminate itself.
- // // Given that is is started without a window so you cannot terminate it
- // // on the desktop, it must terminate itself or you can do it programmatically
- // // from this application using the Kill method.
- // myProcess.WaitForExit();
- //}
- //ChangeParticleVersionNoInDashboard(workStationRelativePath, newVersionNo);
- }
- private static void GitPushAndCommit(string particleRelativePath)
- {
- using (Process myProcess = new Process())
- {
- myProcess.StartInfo.FileName = "CMD.exe";
- myProcess.StartInfo.Arguments = "/C git commit -am \"version updated\"";
- myProcess.StartInfo.WorkingDirectory = particleRelativePath;
- //myProcess.StartInfo.RedirectStandardOutput = true;
- myProcess.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
- myProcess.Start();
- myProcess.WaitForExit();
- }
- }
- private static bool CheckGitPull(string particleRelativePath)
- {
- bool ok = true;
- using (Process myProcess = new Process())
- {
- myProcess.StartInfo.FileName = "CMD.exe";
- myProcess.StartInfo.Arguments = "/C git pull orbitaxcrew development/quark";
- myProcess.StartInfo.WorkingDirectory = particleRelativePath;
- myProcess.StartInfo.RedirectStandardOutput = true;
- myProcess.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
- myProcess.Start();
- string read;
- while((read = myProcess.StandardOutput.ReadLine()) != null)
- {
- if(read.ToLower().Contains("conflict")) {
- ok = false;
- }
- if(read.Contains("conflict")) {
- Console.WriteLine(read, Color.Red);
- }
- else{
- Console.WriteLine(read);
- }
- }
- myProcess.WaitForExit();
- }
- return ok;
- }
- private static bool CheckGitStatus(string particleRelativePath)
- {
- bool ok = true;
- using (Process myProcess = new Process())
- {
- myProcess.StartInfo.FileName = "CMD.exe";
- myProcess.StartInfo.Arguments = "/C git status";
- myProcess.StartInfo.WorkingDirectory = particleRelativePath;
- myProcess.StartInfo.RedirectStandardOutput = true;
- myProcess.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
- myProcess.Start();
- string read;
- while((read = myProcess.StandardOutput.ReadLine()) != null)
- {
- if(read.Contains("git add") || read.Contains("git restore")) {
- ok = false;
- }
- if(read.Contains("modified")) {
- Console.WriteLine(read, Color.Red);
- }
- else{
- Console.WriteLine(read);
- }
- }
- myProcess.WaitForExit();
- }
- return ok;
- }
- private static void ChangeParticleVersionNoInDashboard(string workStationRelativePath, string version)
- {
- var path = GetDashboardRelativePathFromWorkStation(workStationRelativePath);
- FileInfo package;
- foreach (var fileName in Directory.GetFiles(path))
- {
- if (fileName.Contains("package.json"))
- {
- StreamReader reader = new StreamReader(fileName);
- string line = "";
- List<string> lines = new List<string>();
- while ((line = reader.ReadLine()) != null)
- {
- if (line.Contains("\"@orbitax/orbitax-particle\""))
- {
- var temp = line.Replace(line.Split(':')[1].Trim(), "");
- temp += "\"" + version + "\",";
- lines.Add(temp);
- Console.WriteLine(temp);
- continue;
- }
- lines.Add(line);
- }
- reader.Close();
- StreamWriter writer = new StreamWriter(fileName);
- lines.ForEach(t => writer.WriteLine(t));
- writer.Close();
- break;
- }
- }
- }
- private static void CopyDirectories(string sourceDir, string destDir)
- {
- DirectoryInfo source = new DirectoryInfo(sourceDir);
- DirectoryInfo dest = new DirectoryInfo(destDir);
- Directory.CreateDirectory(dest.FullName);
- foreach (var fi in source.GetFiles())
- {
- Console.WriteLine("Copying {0}/{1}", dest.FullName, fi.Name);
- fi.CopyTo(Path.Combine(dest.FullName, fi.Name), true);
- }
- foreach (var di in source.GetDirectories())
- {
- CopyDirectories(di.FullName, destDir + "\\" + di.Name);
- }
- }
- private static string ChangeVersionNumber(string path)
- {
- path = GetParticleRelativePathFromWorkStation(path);
- path += "/apps/particle/projects/particle";
- if (Directory.Exists(path) == false) return "";
- var newVersionNo = "";
- var directory = new DirectoryInfo(path);
- var finalVersion = "";
- foreach (var file in directory.GetFiles())
- {
- if (file.Name == "package.json")
- {
- StreamReader reader = new StreamReader(file.FullName);
- string line = "";
- List<string> lines = new List<string>();
- while ((line = reader.ReadLine()) != null)
- {
- if (line.Contains("version"))
- {
- newVersionNo = findNextVersionNumber(line, out finalVersion);
- lines.Add(newVersionNo);
- }
- else
- lines.Add(line);
- }
- reader.Close();
- StreamWriter writer = new StreamWriter(file.FullName);
- lines.ForEach(t => writer.WriteLine(t));
- writer.Close();
- }
- }
- return finalVersion;
- }
- private static string GetParticleRelativePathFromWorkStation(string path)
- {
- string[] subDirectories = Directory.GetDirectories(path);
- foreach (var subDirectory in subDirectories)
- {
- if (subDirectory.Contains("orbitax-particle"))
- return subDirectory;
- }
- return path;
- }
- private static string GetDashboardRelativePathFromWorkStation(string path)
- {
- string[] subDirectories = Directory.GetDirectories(path);
- foreach (var subDirectory in subDirectories)
- {
- if (subDirectory.Contains("orbitax-dashboard"))
- return subDirectory;
- }
- return path;
- }
- private static string findNextVersionNumber(string lastVersion, out string finalVersion)
- {
- var space = lastVersion.IndexOf('\"');
- lastVersion = lastVersion.Replace("\"version\"", "");
- string pattern = "\".*\"";
- Regex regex = new Regex(pattern);
- var match = regex.Match(lastVersion);
- lastVersion = match.Value.Replace("\"", "");
- var lastTime = DateTime.UtcNow;
- try
- {
- lastTime = DateTime.ParseExact(lastVersion.Split('.')[2].Substring(0, 6), "yyMMdd", System.Globalization.CultureInfo.InvariantCulture);
- }
- catch (Exception ex)
- {
- Console.WriteLine("Existing version convention mismatch");
- }
- var now = DateTime.Now;
- var count = "01";
- if (now.Day == lastTime.Day && now.Month == lastTime.Month && now.Year == lastTime.Year)
- {
- count = lastVersion.Substring(lastVersion.LastIndexOf(".") + 1);
- var nextCount = Int16.Parse(count) + 1;
- count = nextCount.ToString("00");
- }
- var month = now.Month.ToString("00");
- var day = now.Day.ToString("00");
- var year = (now.Year - 2000).ToString("00");
- var newVersion = new String(' ', space) + "\"version\": \"";
- newVersion += "2.0." + year + month + day + count + "-" + now.ToString("MMM").ToLower() + "." + day + "." + count + "\",";
- finalVersion = "2.0." + year + month + day + count + "-" + now.ToString("MMM").ToLower() + "." + day + "." + Int16.Parse(count).ToString();
- return newVersion;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement