Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.58 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Linq;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using Console = Colorful.Console;
  9. using Figlet = Colorful.Figlet;
  10. using FigletFont = Colorful.FigletFont;
  11.  
  12. namespace ParticleBuild
  13. {
  14.     class Program
  15.     {
  16.         public static void Main(string[] arg)
  17.         {
  18.  
  19.              
  20.  
  21.  
  22.             FigletFont font = FigletFont.Load(@"D:\Shahadat\ParticleBuild\ParticleBuild\bin\Debug\netcoreapp3.1\assets\colossal.flf");
  23.             Figlet figlet = new Figlet(font);
  24.  
  25.            
  26.             Console.WriteLine(figlet.ToAscii("Build Success"), ColorTranslator.FromHtml("#90ee90"));
  27.  
  28.             var workStationRelativePath = @"D:\Shahadat\Workstation";
  29.             var particleRelativePath = GetParticleRelativePathFromWorkStation(workStationRelativePath);
  30.  
  31.  
  32.             bool ok = CheckGitStatus(particleRelativePath);
  33.             if(ok == false) {
  34.                 Console.WriteLine("Commit your code or Stash them", Color.Red);
  35.                 return;
  36.             }
  37.  
  38.  
  39.             ok = CheckGitPull(particleRelativePath);
  40.             if(ok == false) {
  41.                 Console.WriteLine("Resolve Merge Conflict", Color.Red);
  42.                 return;
  43.             }
  44.  
  45.             using (Process myProcess = new Process())
  46.             {
  47.                myProcess.StartInfo.FileName = "CMD.exe";
  48.                myProcess.StartInfo.Arguments = "/C git status";
  49.                myProcess.StartInfo.WorkingDirectory = GetParticleRelativePathFromWorkStation(workStationRelativePath);
  50.                myProcess.StartInfo.RedirectStandardOutput = true;
  51.                myProcess.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
  52.                myProcess.Start();
  53.                myProcess.BeginOutputReadLine();
  54.  
  55.                //StreamReader reader =  myProcess.StandardOutput.ReadLine();
  56.                myProcess.WaitForExit();
  57.             }
  58.  
  59.             Console.WriteLine("Version changing...");
  60.             var newVersionNo = ChangeVersionNumber(workStationRelativePath);
  61.             if (newVersionNo.Length <= 20)
  62.             {
  63.                 Console.WriteLine("Error occured in version changing");
  64.                 return;
  65.             }
  66.             Console.WriteLine("New version: " + newVersionNo.Trim());
  67.             GitPushAndCommit(particleRelativePath);
  68.  
  69.  
  70.  
  71.             //Console.WriteLine("Production build running...");
  72.             //using (Process myProcess = new Process())
  73.             //{
  74.             //    myProcess.StartInfo.FileName = "CMD.exe";
  75.             //    myProcess.StartInfo.Arguments = "/C cd " + GetParticleRelativePathFromWorkStation(workStationRelativePath) + "/apps/particle && " + "npm run bmax";
  76.             //    myProcess.Start();
  77.             //    // This code assumes the process you are starting will terminate itself.
  78.             //    // Given that is is started without a window so you cannot terminate it
  79.             //    // on the desktop, it must terminate itself or you can do it programmatically
  80.             //    // from this application using the Kill method.
  81.             //    myProcess.WaitForExit();
  82.             //}
  83.  
  84.  
  85.  
  86.             // string sourceDir = @"D:\Shahadat\Workstation\orbitax-particle_fork-shahadat\apps\particle\projects\particle\src\lib\assets";
  87.             // string destDir = @"D:\Shahadat\Workstation\orbitax-particle_fork-shahadat\apps\particle\dist\particle\lib\assets";
  88.             // CopyDirectories(sourceDir, destDir);
  89.                
  90.  
  91.             //Console.WriteLine("Finished Copy");
  92.             //Console.WriteLine("Now publish him");
  93.  
  94.             //using (Process myProcess = new Process())
  95.             //{
  96.             //    myProcess.StartInfo.WorkingDirectory = @"D:\Shahadat\Workstation\orbitax-particle_fork-shahadat\apps\particle\dist\particle";
  97.             //    myProcess.StartInfo.FileName = "CMD.exe";
  98.             //    myProcess.StartInfo.Arguments = "/C npm install";
  99.             //    myProcess.Start();
  100.             //    // This code assumes the process you are starting will terminate itself.
  101.             //    // Given that is is started without a window so you cannot terminate it
  102.             //    // on the desktop, it must terminate itself or you can do it programmatically
  103.             //    // from this application using the Kill method.
  104.             //    myProcess.WaitForExit();
  105.             //}
  106.  
  107.             //ChangeParticleVersionNoInDashboard(workStationRelativePath, newVersionNo);
  108.         }
  109.  
  110.         private static void GitPushAndCommit(string particleRelativePath)
  111.         {
  112.             using (Process myProcess = new Process())
  113.             {
  114.                myProcess.StartInfo.FileName = "CMD.exe";
  115.                myProcess.StartInfo.Arguments = "/C git commit -am \"version updated\"";
  116.                myProcess.StartInfo.WorkingDirectory = particleRelativePath;
  117.                //myProcess.StartInfo.RedirectStandardOutput = true;
  118.                myProcess.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
  119.                myProcess.Start();
  120.            
  121.                 myProcess.WaitForExit();
  122.             }
  123.      }
  124.  
  125.         private static bool CheckGitPull(string particleRelativePath)
  126.         {
  127.             bool ok = true;
  128.             using (Process myProcess = new Process())
  129.             {
  130.                myProcess.StartInfo.FileName = "CMD.exe";
  131.                myProcess.StartInfo.Arguments = "/C git pull orbitaxcrew development/quark";
  132.                myProcess.StartInfo.WorkingDirectory = particleRelativePath;
  133.                myProcess.StartInfo.RedirectStandardOutput = true;
  134.                myProcess.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
  135.                myProcess.Start();
  136.                string read;
  137.                while((read = myProcess.StandardOutput.ReadLine()) != null)
  138.                {
  139.                    if(read.ToLower().Contains("conflict")) {
  140.                        ok = false;
  141.                    }
  142.                    if(read.Contains("conflict")) {
  143.                        Console.WriteLine(read, Color.Red);
  144.                    }
  145.                    else{
  146.                         Console.WriteLine(read);
  147.                    }
  148.                }
  149.                 myProcess.WaitForExit();
  150.  
  151.             }
  152.             return ok;
  153.         }
  154.  
  155.         private static bool CheckGitStatus(string particleRelativePath)
  156.         {
  157.             bool ok = true;
  158.             using (Process myProcess = new Process())
  159.             {
  160.                myProcess.StartInfo.FileName = "CMD.exe";
  161.                myProcess.StartInfo.Arguments = "/C git status";
  162.                myProcess.StartInfo.WorkingDirectory = particleRelativePath;
  163.                myProcess.StartInfo.RedirectStandardOutput = true;
  164.                myProcess.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
  165.                myProcess.Start();
  166.                string read;
  167.                while((read = myProcess.StandardOutput.ReadLine()) != null)
  168.                {
  169.                    if(read.Contains("git add") || read.Contains("git restore")) {
  170.                        ok = false;
  171.                    }
  172.                    if(read.Contains("modified")) {
  173.                        Console.WriteLine(read, Color.Red);
  174.                    }
  175.                    else{
  176.                         Console.WriteLine(read);
  177.                    }
  178.                }
  179.                 myProcess.WaitForExit();
  180.  
  181.             }
  182.             return ok;
  183.  
  184.         }
  185.  
  186.         private static void ChangeParticleVersionNoInDashboard(string workStationRelativePath, string version)
  187.         {
  188.             var path = GetDashboardRelativePathFromWorkStation(workStationRelativePath);
  189.             FileInfo package;
  190.             foreach (var fileName in Directory.GetFiles(path))
  191.             {
  192.                 if (fileName.Contains("package.json"))
  193.                 {
  194.                     StreamReader reader = new StreamReader(fileName);
  195.                     string line = "";
  196.                     List<string> lines = new List<string>();
  197.                     while ((line = reader.ReadLine()) != null)
  198.                     {
  199.                         if (line.Contains("\"@orbitax/orbitax-particle\""))
  200.                         {
  201.                             var temp = line.Replace(line.Split(':')[1].Trim(), "");
  202.                             temp += "\"" + version + "\",";
  203.                             lines.Add(temp);
  204.                             Console.WriteLine(temp);
  205.                             continue;
  206.  
  207.                         }
  208.                         lines.Add(line);
  209.                     }
  210.                     reader.Close();
  211.  
  212.                     StreamWriter writer = new StreamWriter(fileName);
  213.                     lines.ForEach(t => writer.WriteLine(t));
  214.                     writer.Close();
  215.                     break;
  216.                 }
  217.             }
  218.  
  219.  
  220.         }
  221.  
  222.         private static void CopyDirectories(string sourceDir, string destDir)
  223.         {
  224.             DirectoryInfo source = new DirectoryInfo(sourceDir);
  225.             DirectoryInfo dest = new DirectoryInfo(destDir);
  226.             Directory.CreateDirectory(dest.FullName);
  227.  
  228.             foreach (var fi in source.GetFiles())
  229.             {
  230.                 Console.WriteLine("Copying {0}/{1}", dest.FullName, fi.Name);
  231.                 fi.CopyTo(Path.Combine(dest.FullName, fi.Name), true);
  232.             }
  233.  
  234.             foreach (var di in source.GetDirectories())
  235.             {
  236.                 CopyDirectories(di.FullName, destDir + "\\" + di.Name);
  237.             }
  238.         }
  239.  
  240.         private static string ChangeVersionNumber(string path)
  241.         {
  242.             path = GetParticleRelativePathFromWorkStation(path);
  243.             path += "/apps/particle/projects/particle";
  244.             if (Directory.Exists(path) == false) return "";
  245.                 var newVersionNo = "";
  246.             var directory = new DirectoryInfo(path);
  247.             var finalVersion = "";
  248.             foreach (var file in directory.GetFiles())
  249.             {
  250.                 if (file.Name == "package.json")
  251.                 {
  252.                     StreamReader reader = new StreamReader(file.FullName);
  253.                     string line = "";
  254.                     List<string> lines = new List<string>();
  255.                     while ((line = reader.ReadLine()) != null)
  256.                     {
  257.                         if (line.Contains("version"))
  258.                         {
  259.                             newVersionNo = findNextVersionNumber(line, out finalVersion);
  260.                             lines.Add(newVersionNo);
  261.                         }
  262.                         else
  263.                             lines.Add(line);
  264.                     }
  265.                     reader.Close();
  266.  
  267.                     StreamWriter writer = new StreamWriter(file.FullName);
  268.                     lines.ForEach(t => writer.WriteLine(t));
  269.                     writer.Close();
  270.  
  271.                 }
  272.             }
  273.             return finalVersion;
  274.         }
  275.  
  276.         private static string GetParticleRelativePathFromWorkStation(string path)
  277.         {
  278.             string[] subDirectories = Directory.GetDirectories(path);
  279.             foreach (var subDirectory in subDirectories)
  280.             {
  281.                 if (subDirectory.Contains("orbitax-particle"))
  282.                     return subDirectory;
  283.             }
  284.             return path;
  285.         }
  286.  
  287.         private static string GetDashboardRelativePathFromWorkStation(string path)
  288.         {
  289.             string[] subDirectories = Directory.GetDirectories(path);
  290.             foreach (var subDirectory in subDirectories)
  291.             {
  292.                 if (subDirectory.Contains("orbitax-dashboard"))
  293.                     return subDirectory;
  294.             }
  295.             return path;
  296.         }
  297.  
  298.         private static string findNextVersionNumber(string lastVersion, out string finalVersion)
  299.         {
  300.             var space = lastVersion.IndexOf('\"');
  301.  
  302.             lastVersion = lastVersion.Replace("\"version\"", "");
  303.             string pattern = "\".*\"";
  304.             Regex regex = new Regex(pattern);
  305.  
  306.             var match = regex.Match(lastVersion);
  307.             lastVersion = match.Value.Replace("\"", "");
  308.  
  309.             var lastTime = DateTime.UtcNow;
  310.             try
  311.             {
  312.                 lastTime = DateTime.ParseExact(lastVersion.Split('.')[2].Substring(0, 6), "yyMMdd", System.Globalization.CultureInfo.InvariantCulture);
  313.             }
  314.             catch (Exception ex)
  315.             {
  316.                 Console.WriteLine("Existing version convention mismatch");
  317.             }
  318.  
  319.             var now = DateTime.Now;
  320.             var count = "01";
  321.             if (now.Day == lastTime.Day && now.Month == lastTime.Month && now.Year == lastTime.Year)
  322.             {
  323.                 count = lastVersion.Substring(lastVersion.LastIndexOf(".") + 1);
  324.                 var nextCount = Int16.Parse(count) + 1;
  325.                 count = nextCount.ToString("00");
  326.             }
  327.  
  328.             var month = now.Month.ToString("00");
  329.             var day = now.Day.ToString("00");
  330.             var year = (now.Year - 2000).ToString("00");
  331.  
  332.             var newVersion = new String(' ', space) + "\"version\": \"";
  333.             newVersion += "2.0." + year + month + day + count + "-" + now.ToString("MMM").ToLower() + "." + day + "." + count + "\",";
  334.             finalVersion = "2.0." + year + month + day + count + "-" + now.ToString("MMM").ToLower() + "." + day + "." + Int16.Parse(count).ToString();
  335.             return newVersion;
  336.         }
  337.     }
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement