public bool Generate(CommandConfiguration configuration, IOutput output)
        {
            Logger.Trace("Execute watchdog command...");
            if (configuration.Parameters.GetBool("async"))
            {
                Logger.Trace("Start generation in separate process...");
                if (InstanceHelper.IsRunning())
                {
                    Logger.Trace("Generation aborted. An other watchdog is already running.");
                    return(true);
                }
                string           arguments = string.Join(" ", configuration.Parameters.Where(x => x.Name != "async"));
                ProcessStartInfo startInfo = new ProcessStartInfo(Assembly.GetEntryAssembly().Location, $"watchdog {arguments}");
                startInfo.WindowStyle      = ProcessWindowStyle.Hidden;
                startInfo.WorkingDirectory = FileSystem.Parent(startInfo.FileName);
                Logger.Trace($"{startInfo.FileName} {startInfo.Arguments}");
                Process.Start(startInfo);
                return(true);
            }

            string   url            = configuration.Parameters.GetString("url");
            string   launchSettings = configuration.Parameters.GetString("launchSettings");
            TimeSpan timeout        = configuration.Parameters.GetTimeSpan("timeout", TimeSpan.FromMinutes(5));
            TimeSpan delay          = configuration.Parameters.GetTimeSpan("delay", TimeSpan.FromSeconds(1));
            TimeSpan sleep          = configuration.Parameters.GetTimeSpan("sleep", TimeSpan.FromMilliseconds(100));
            int      tries          = configuration.Parameters.GetInt("tries");

            string command = configuration.Parameters.GetString("command");

            if (string.IsNullOrEmpty(command))
            {
                Logger.Error("Command can not be empty");
                return(false);
            }
            if (string.IsNullOrEmpty(url) && string.IsNullOrEmpty(launchSettings))
            {
                Logger.Error("No valid target found. Add at least a -url=... or a -launchSettings=... parameter");
                return(false);
            }
            if (!string.IsNullOrEmpty(launchSettings))
            {
                LaunchSettingsReader reader = new LaunchSettingsReader();
                url = reader.ReadApplicationUrl(launchSettings);
                if (string.IsNullOrEmpty(url))
                {
                    Logger.Error("No value for iisSettings/iisExpress/applicationUrl in launchSettings.json found");
                    return(false);
                }
                url += "/api/v1/generator/available";
            }
            HttpWatchdog watchdog = new HttpWatchdog(url, tries, delay, sleep, timeout);
            bool         success  = watchdog.WaitAsync().Result;

            if (success)
            {
                CommandConfiguration nextCommand = new CommandConfiguration(command);
                nextCommand.CopyBaseFrom(configuration);
                nextCommand.Parameters.AddRange(configuration.Parameters.Where(x => x.Name.StartsWith(command)).Select(x => this.MapParameter(x, command)));
                nextCommand.ReadFromParameters(nextCommand.Parameters, this.resolver.Get <List <ILanguage> >());

                this.resolver.Get <CommandRunner>().Run(nextCommand, output);
            }
            return(true);
        }
Example #2
0
        public override IGeneratorCommandResult Run()
        {
            Logger.Trace("Execute watchdog command...");
            if (this.Parameters.IsAsync)
            {
                Logger.Trace("Start generation in separate process...");
                if (InstanceHelper.IsRunning())
                {
                    Logger.Trace("Generation aborted. An other watchdog is already running.");
                    return(this.Success());
                }
                string           arguments = string.Join(" ", this.Parameters);
                ProcessStartInfo startInfo = new ProcessStartInfo(Assembly.GetEntryAssembly().Location, $"watchdog {arguments}");
                startInfo.WindowStyle      = ProcessWindowStyle.Hidden;
                startInfo.WorkingDirectory = FileSystem.Parent(startInfo.FileName);
                Logger.Trace($"{startInfo.FileName} {startInfo.Arguments}");
                Process.Start(startInfo);
                return(this.Success());
            }

            string   url            = this.Parameters.Url;
            string   launchSettings = this.Parameters.LaunchSettings;
            TimeSpan timeout        = this.Parameters.Timeout;
            TimeSpan delay          = this.Parameters.Delay;
            TimeSpan sleep          = this.Parameters.Sleep;
            int      tries          = this.Parameters.Tries;

            string command = this.Parameters.Command;

            if (string.IsNullOrEmpty(command))
            {
                Logger.Error("Command can not be empty");
                return(this.Error());
            }
            if (string.IsNullOrEmpty(url) && string.IsNullOrEmpty(launchSettings))
            {
                Logger.Error("No valid target found. Add at least a -url=... or a -launchSettings=... parameter");
                return(this.Error());
            }
            if (!string.IsNullOrEmpty(launchSettings))
            {
                LaunchSettingsReader reader = new LaunchSettingsReader();
                url = reader.ReadApplicationUrl(launchSettings);
                if (string.IsNullOrEmpty(url))
                {
                    Logger.Error("No value for iisSettings/iisExpress/applicationUrl in launchSettings.json found");
                    return(this.Error());
                }
                url += "/api/v1/generator/available";
            }
            HttpWatchdog watchdog = new HttpWatchdog(url, tries, delay, sleep, timeout);
            bool         success  = watchdog.WaitAsync().Result;

            if (success)
            {
                throw new NotImplementedException();
                //CommandConfiguration nextCommand = new CommandConfiguration(command);
                //nextCommand.Parameters.AddRange(configuration.Parameters.Where(x => x.Name.StartsWith(command)).Select(x => this.MapParameter(x, command)));
                //nextCommand.ReadFromParameters(nextCommand.Parameters, this.resolver.Get<List<ILanguage>>());

                //this.resolver.Get<CommandRunner>().Run(nextCommand, output);
            }
            return(this.Success());
        }
Example #3
0
        public bool Generate(CommandConfiguration configuration, IOutput output)
        {
            string outputPath = configuration.Parameters.GetString("output");

            if (!string.IsNullOrEmpty(outputPath))
            {
                output = new FileOutput(outputPath);
            }
            if (output == null)
            {
                Logger.Error("No valid output specified");
                return(false);
            }

            string url            = configuration.Parameters.GetString("url");
            string launchSettings = configuration.Parameters.GetString("launchSettings");

            if (string.IsNullOrEmpty(url) && string.IsNullOrEmpty(launchSettings))
            {
                Logger.Error("No valid target found. Add at least a -url=... or a -launchSettings=... parameter");
                return(false);
            }
            if (!string.IsNullOrEmpty(launchSettings))
            {
                LaunchSettingsReader reader = new LaunchSettingsReader();
                url = reader.ReadApplicationUrl(launchSettings);
                if (string.IsNullOrEmpty(url))
                {
                    Logger.Error("No value for iisSettings/iisExpress/applicationUrl in launchSettings.json found");
                    return(false);
                }
            }

            string path = configuration.Parameters.GetString("path");
            string clientConfiguration = FileSystem.ReadAllText(PathResolver.Resolve(path, configuration));

            HttpClient client = new HttpClient();
            MultipartFormDataContent createContent = new MultipartFormDataContent();

            createContent.Add(new StringContent(clientConfiguration), "configuration");
            Logger.Trace($"Request {url}/generator/create");
            HttpResponseMessage createResponse = client.PostAsync($"{url}/generator/create", createContent).Result;

            if (!createResponse.IsSuccessStatusCode)
            {
                Logger.Error($"Connection to generator failed: {createResponse.StatusCode}");
                return(false);
            }
            string id = createResponse.Content.ReadAsStringAsync().Result;
            MultipartFormDataContent getFilesContent = new MultipartFormDataContent();

            getFilesContent.Add(new StringContent(id), "id");
            Logger.Trace($"Request {url}/generator/getFiles");
            HttpResponseMessage getFilesResponse = client.PostAsync($"{url}/generator/getFiles", getFilesContent).Result;

            if (!getFilesResponse.IsSuccessStatusCode)
            {
                Logger.Error($"Connection to generator failed: {getFilesResponse.StatusCode}");
                return(false);
            }
            string[] filePaths = getFilesResponse.Content.ReadAsStringAsync().Result.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            if (filePaths.Length == 0)
            {
                Logger.Warning("No files generated");
            }
            foreach (string filePath in filePaths)
            {
                MultipartFormDataContent getFileContent = new MultipartFormDataContent();
                getFileContent.Add(new StringContent(id), "id");
                getFileContent.Add(new StringContent(filePath), "path");
                Logger.Trace($"Request {url}/generator/getFile");
                HttpResponseMessage getFileResponse = client.PostAsync($"{url}/generator/getFile", getFileContent).Result;
                output.Write(filePath, getFileResponse.Content.ReadAsStringAsync().Result);
            }
            return(true);
        }