Exemple #1
0
        /// <summary>
        /// Start script.
        /// </summary>
        /// <param name="isRedirectStdErr">If true, do not write to stderr. Use this flag if shell command is known to write info (mistakenly) to stderr.</param>
        internal static void Start(string workingDirectory, string fileName, string arguments, bool isWait = true, bool isRedirectStdErr = false)
        {
            string time = UtilFramework.DateTimeToString(DateTime.Now);

            UtilCli.ConsoleWriteLinePassword(string.Format("### {4} Process Begin (FileName={1}; Arguments={2}; IsWait={3}; WorkingDirectory={0};)", workingDirectory, fileName, arguments, isWait, time), ConsoleColor.Green);

            ProcessStartInfo info = new ProcessStartInfo
            {
                WorkingDirectory = workingDirectory,
                FileName         = fileName,
                Arguments        = arguments
            };

            if (isRedirectStdErr)
            {
                info.RedirectStandardError = true; // Do not write to stderr.
            }
            // info.UseShellExecute = true;

            using (var process = Process.Start(info))
            {
                if (isWait)
                {
                    if (isRedirectStdErr)
                    {
                        // process.WaitForExit(); // Can hang. For example Angular 9.1.1 build:ssr (May be when std buffer is full)
                        string errorText = process.StandardError.ReadToEnd(); // Waits for process to exit.
                        process.WaitForExit();                                // Used for Ubuntu. Otherwise HasExited is not (yet) true.
                        UtilFramework.Assert(process.HasExited);
                        if (!string.IsNullOrEmpty(errorText))
                        {
                            UtilCli.ConsoleWriteLinePassword(string.Format("### {4} Process StdErr (FileName={1}; Arguments={2}; IsWait={3}; WorkingDirectory={0};)", workingDirectory, fileName, arguments, isWait, time), ConsoleColor.DarkGreen); // Write stderr to stdout.
                            UtilCli.ConsoleWriteLinePassword(errorText, ConsoleColor.DarkGreen);                                                                                                                                                     // Log DarkGreen because it is not treated like an stderr output.
                        }
                    }
                    else
                    {
                        process.WaitForExit();
                        UtilFramework.Assert(process.HasExited);
                    }
                    if (process.ExitCode != 0)
                    {
                        throw new Exception("Script failed!");
                    }
                }
            }

            UtilCli.ConsoleWriteLinePassword(string.Format("### {4} Process End (FileName={1}; Arguments={2}; IsWait={3}; WorkingDirectory={0};)", workingDirectory, fileName, arguments, isWait, time), ConsoleColor.DarkGreen);
        }
Exemple #2
0
        /// <summary>
        /// Tag version build.
        /// </summary>
        internal static void VersionBuild(Action build)
        {
            // Read UtilFramework.cs
            string fileNameServer = UtilFramework.FolderName + "Framework/Framework/UtilFramework.cs";
            string textServer     = UtilFramework.FileLoad(fileNameServer);
            string fileNameClient = UtilFramework.FolderName + "Framework/Framework.Angular/application/src/app/data.service.ts";
            string textClient     = UtilFramework.FileLoad(fileNameClient);

            string versionBuild = string.Format("Build (WorkplaceX={3}; Commit={0}; Pc={1}; Time={2} (UTC);)", UtilCli.GitCommit(), System.Environment.MachineName, UtilFramework.DateTimeToString(DateTime.Now.ToUniversalTime()), UtilFramework.Version);

            string findServer    = "/* VersionBuild */"; // See also: method CommandBuild.BuildServer();
            string replaceServer = string.Format("                return \"{0}\"; /* VersionBuild */", versionBuild);
            string findClient    = "/* VersionBuild */"; // See also: file data.service.ts
            string replaceClient = string.Format("  public VersionBuild: string = \"{0}\"; /* VersionBuild */", versionBuild);

            // Write UtilFramework.cs
            string textNewServer = UtilFramework.ReplaceLine(textServer, findServer, replaceServer);

            File.WriteAllText(fileNameServer, textNewServer);
            string textNewClient = UtilFramework.ReplaceLine(textClient, findClient, replaceClient);

            File.WriteAllText(fileNameClient, textNewClient);

            try
            {
                build();
            }
            finally
            {
                File.WriteAllText(fileNameServer, textServer); // Back to original text.
                File.WriteAllText(fileNameClient, textClient); // Back to original text.
            }
        }
        protected internal override void Execute()
        {
            // Build angular client
            if (!UtilCli.FolderNameExist(UtilFramework.FolderName + "Application.Server/Framework/Framework.Angular/"))
            {
                var commandBuild = new CommandBuild(AppCli);
                UtilCli.OptionSet(ref commandBuild.OptionClientOnly, true);
                commandBuild.Execute();
            }

            if (optionWatch.OptionGet())
            {
                {
                    ConfigCli configCli = ConfigCli.Load();

                    Console.WriteLine("Select Website:");

                    for (int i = 0; i < configCli.WebsiteList.Count; i++)
                    {
                        Console.WriteLine(string.Format("{0}={1}", i + 1, configCli.WebsiteList[i].FolderNameNpmBuild));
                    }

                    Console.Write("Website: ");

                    var websiteIndex = int.Parse(Console.ReadLine()) - 1;

                    var website = configCli.WebsiteList[websiteIndex];

                    string folderNameNpmBuilt = UtilFramework.FolderName + website.FolderNameNpmBuild;
                    string folderNameDist     = UtilFramework.FolderName + website.FolderNameDist;

                    string folderNameAngular         = UtilFramework.FolderName + "Framework/Framework.Angular/application/";
                    string folderNameCustomComponent = UtilFramework.FolderName + "Application.Website/Shared/CustomComponent/";

                    Console.WriteLine("Copy folder dist/");
                    UtilCli.FolderCopy(folderNameDist, folderNameAngular + "src/Application.Website/dist/", "*.*", true);

                    Console.WriteLine("Copy folder CustomComponent/");
                    UtilCli.FolderCopy(folderNameCustomComponent, folderNameAngular + "src/Application.Website/Shared/CustomComponent/", "*.*", true);

                    bool isWebsiteWatch = UtilCli.ConsoleReadYesNo("Start Website npm watch?");
                    bool isFileSync     = UtilCli.ConsoleReadYesNo("Start Website FileSync (dist/ to Angular)?");
                    bool isAngular      = UtilCli.ConsoleReadYesNo("Start Angular?");
                    bool isServer       = UtilCli.ConsoleReadYesNo("Start Server?");

                    // FileSync
                    if (isFileSync)
                    {
                        FileSync fileSync = new FileSync();
                        fileSync.AddFolder(folderNameDist, folderNameAngular + "src/Application.Website/dist/");
                        fileSync.AddFolder(folderNameAngular + "src/Application.Website/Shared/CustomComponent/", folderNameCustomComponent);
                    }

                    // Website --watch
                    if (isWebsiteWatch)
                    {
                        UtilCli.Npm(folderNameNpmBuilt, "run build -- --watch", isWait: false);
                    }

                    // Angular client
                    if (isAngular)
                    {
                        UtilCli.Npm(folderNameAngular, "start -- --disable-host-check", isWait: false); // disable-host-check to allow for example http://localhost2:4200/
                    }

                    // .NET Server
                    if (isServer)
                    {
                        string folderName = UtilFramework.FolderName + @"Application.Server/";
                        UtilCli.DotNet(folderName, "run", isWait: false);
                    }

                    void heartBeat()
                    {
                        Console.WriteLine();
                        Console.WriteLine(UtilFramework.DateTimeToString(DateTime.Now));
                        if (isAngular)
                        {
                            UtilCli.ConsoleWriteLineColor("Angular: http://" + website.DomainNameList.First().DomainName + ":4200/", System.ConsoleColor.Green);
                        }
                        if (isServer)
                        {
                            UtilCli.ConsoleWriteLineColor("Server: http://" + website.DomainNameList.First().DomainName + ":50919/", System.ConsoleColor.Green);
                        }
                        if (isFileSync)
                        {
                            UtilCli.ConsoleWriteLineColor("Modify Website: " + folderNameNpmBuilt, System.ConsoleColor.Green);
                            UtilCli.ConsoleWriteLineColor("Modify CustomComponent: " + folderNameCustomComponent, System.ConsoleColor.Green);
                        }
                        if (isAngular)
                        {
                            UtilCli.ConsoleWriteLineColor("Modify Angular: " + folderNameAngular, System.ConsoleColor.Green);
                        }
                        Task.Delay(5000).ContinueWith((Task task) => heartBeat());
                    }

                    if (isFileSync || isWebsiteWatch || isAngular || isServer)
                    {
                        heartBeat();
                        while (true)
                        {
                            Console.ReadLine(); // Program would end and with it FileSync
                        }
                    }
                }
            }
            else
            {
                string folderName = UtilFramework.FolderName + @"Application.Server/";
                UtilCli.VersionBuild(() =>
                {
                    UtilCli.DotNet(folderName, "build");
                });
                UtilCli.DotNet(folderName, "run --no-build", false);
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    UtilCli.OpenWebBrowser("http://localhost:50919/"); // For port setting see also: Application.Server\Properties\launchSettings.json (applicationUrl, sslPort)
                }
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    // Ubuntu list all running processes: 'ps'
                    // To reboot Ubuntu type on Windows command prompt: 'wsl -t Ubuntu-18.04'
                    // Ubuntu show processes tool: 'htop'
                    UtilCli.ConsoleWriteLineColor("Stop server with command 'killall -SIGKILL Application.Server node dotnet'", System.ConsoleColor.Yellow);
                }
            }
        }