internal static void FolderDelete(string folderName) { var count = 0; do { if (count > 0) { Task.Delay(1000).Wait(); // Wait for next attempt. } if (UtilCli.FolderNameExist(folderName)) { foreach (FileInfo fileInfo in new DirectoryInfo(folderName).GetFiles("*.*", SearchOption.AllDirectories)) { fileInfo.Attributes = FileAttributes.Normal; // See also: https://stackoverflow.com/questions/1701457/directory-delete-doesnt-work-access-denied-error-but-under-windows-explorer-it/30673648 } try { Directory.Delete(folderName, true); } catch (IOException) { // Silent exception. Console.WriteLine(string.Format("Can not delete folder! ({0})", folderName)); } } count += 1; } while (UtilCli.FolderNameExist(folderName) && count <= 3); UtilFramework.Assert(!UtilCli.FolderNameExist(folderName), string.Format("Can not delete folder! Make sure server.ts and node.exe is not running. ({0}", folderName)); }
/// <summary> /// Run npm command. /// </summary> internal static void Npm(string workingDirectory, string arguments, bool isWait = true, bool isRedirectStdErr = false) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { UtilCli.Start(workingDirectory, "cmd", "/c npm.cmd " + arguments, isWait: isWait, isRedirectStdErr: isRedirectStdErr); } else { UtilCli.Start(workingDirectory, "npm", arguments, isWait: isWait, isRedirectStdErr: isRedirectStdErr); } }
/// <summary> /// Copy ConfigCli.json to ConfigServer.json and validate ConnectionString exists. /// </summary> private void CopyConfigCliToConfigServer() { ConfigCli.CopyConfigCliToConfigServer(); var configCli = ConfigCli.Load(); var environment = configCli.EnvironmentGet(); if (UtilFramework.StringNull(environment.ConnectionStringFramework) == null || UtilFramework.StringNull(environment.ConnectionStringFramework) == null) { UtilCli.ConsoleWriteLineColor(string.Format("No ConnectionString for {0}! Set it with command cli config connectionString.", environment.EnvironmentName), ConsoleColor.Yellow); } }
/// <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); }
/// <summary> /// Returns git commit sha. /// </summary> internal static string GitCommit() { string result = "Commit"; try { result = UtilCli.StartStdout(UtilFramework.FolderName, "git", "rev-parse --short HEAD"); result = result.Replace("\n", ""); } catch { // Silent exception } return(result); }
/// <summary> /// Run command line interface. /// </summary> public void Run(string[] args) { Title(args); try { ConfigCli.Init(this); var configCli = ConfigCli.Load(); ConfigCli.Save(configCli); // Reset ConfigCli.json ConfigCli.CopyConfigCliToConfigServer(); CommandEnvironment.ConsoleWriteLineCurrentEnvironment(configCli); commandLineApplication.Execute(args); CopyConfigCliToConfigServer(); // Copy new values from ConfigCli.json to ConfigServer.json } catch (Exception exception) // For example unrecognized option { UtilCli.ConsoleWriteLineError(exception); Environment.ExitCode = 1; // echo %errorlevel% } }
private void RegisterCommandInit() { foreach (CommandBase command in CommandList) { commandLineApplication.Command(command.Name, (configuration) => { configuration.Description = command.Description; command.Configuration = configuration; command.Register(configuration); configuration.OnExecute(() => { UtilCli.ConsoleWriteLineColor($"Execute Framework Command Cli ({command.Name})", ConsoleColor.Green); command.Execute(); return(0); }); configuration.HelpOption("-h | --help"); // Command help (to show arguments and options) }); } commandLineApplication.HelpOption("-h | --help"); // Command line interface help (to show commands) }
/// <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. } }
/// <summary> /// Copy folder. /// </summary> internal void FolderCopy(string folderNameSource, string folderNameDest) { UtilCli.FolderDelete(folderNameDest); UtilCli.FolderCopy(folderNameSource, folderNameDest, "*.*", true); }
/// <summary> /// Copy file from source to dest. Creates new dest folder if it doesn't exist. /// </summary> public void FileCopy(string fileNameSource, string fileNameDest) { UtilCli.FileCopy(fileNameSource, fileNameDest); }