public void OpenGitGenerationWindow(GitGenerationSettings settings, List <IProjectData> selectedProjects, Action onConfirm)
        {
            if (!GitGenerationWindow.IsVisible)
            {
                onGitGenerationConfirmed = onConfirm;

                if (gitGenerationWindow == null)
                {
                    gitGenerationWindow = new GitGenerationWindow();
                }

                if (selectedProjects != null && selectedProjects.Count > 0)
                {
                    Controller.Data.LockScreenVisibility = Visibility.Visible;
                    gitGenerationWindow.Init(this, settings, selectedProjects);
                    gitGenerationWindow.Owner = this;
                    gitGenerationWindow.Show();
                }
                else
                {
                    if (gitGenerationWindow.IsVisible)
                    {
                        gitGenerationWindow.Hide();
                    }
                }

                if (settings.TemplateSettings.Count <= 0)
                {
                    Log.Here().Error("Template settings are empty!");
                }
            }
        }
        public void Init(MainWindow ParentWindow, GitGenerationSettings GenerationSettings, List <IProjectData> SelectedProjects)
        {
            mainWindow         = ParentWindow;
            generationSettings = GenerationSettings;
            this.DataContext   = generationSettings;

            if (GenerationSettings.ExportProjects == null)
            {
                var list = new ObservableImmutableList <IProjectData>();
                GenerationSettings.SetExportProjects(list);
            }
            else
            {
                GenerationSettings.ExportProjects.Clear();
            }

            SelectedProjects.ForEach((IProjectData project) => GenerationSettings.ExportProjects.Add(project));
        }
 public void StartGitGeneration(GitGenerationSettings settings)
 {
     Controller.Data.LockScreenVisibility = Visibility.Collapsed;
     onGitGenerationConfirmed?.Invoke();
 }
示例#4
0
        private async Task <bool> GenerateGitFilesAsync(DefaultProjectData modProject, GitGenerationSettings generationSettings, int endPercentage)
        {
            if (modProject == null)
            {
                return(false);
            }

            int percentageIncrement = endPercentage / 9;

            Log.Here().Activity($"[Progress] percentageIncrement is {percentageIncrement} / {endPercentage}");

            if (!string.IsNullOrEmpty(Data.Settings.GitRootDirectory))
            {
                string gitProjectRootDirectory = Path.Combine(Data.Settings.GitRootDirectory, modProject.ProjectName);

                AppController.Main.UpdateProgress(percentageIncrement, "Creating git project directory...");

                var rootRepoDirectory = Directory.CreateDirectory(gitProjectRootDirectory);

                if (!Directory.Exists(gitProjectRootDirectory))
                {
                    Directory.CreateDirectory(gitProjectRootDirectory);
                }

                if (generationSettings.InitGit)
                {
                    AppController.Main.UpdateProgress(percentageIncrement, "Initializing git repo...");

                    var result = await GitGenerator.InitRepository(gitProjectRootDirectory, Data.Settings.DefaultAuthor).ConfigureAwait(false);

                    if (result)
                    {
                        Log.Here().Activity("Created git repository for project ({0}) at {1}", modProject.ProjectName, gitProjectRootDirectory);
                    }
                    else
                    {
                        Log.Here().Error("Error creating git repository for project {0}.", modProject.ProjectName);
                    }
                }

                bool   commitGit     = false;
                string commitMessage = "";

                if (generationSettings.TemplateSettings != null && generationSettings.TemplateSettings.Count > 0)
                {
                    AppController.Main.UpdateProgress(percentageIncrement, "Generating templates...");

                    foreach (var templateSetting in generationSettings.TemplateSettings)
                    {
                        var templateData = Data.Templates.Where(t => t.Name == templateSetting.TemplateName).FirstOrDefault();
                        if (templateData != null)
                        {
                            if (templateSetting.Enabled)
                            {
                                string outputFilePath = Path.Combine(gitProjectRootDirectory, templateData.ExportPath);
                                string outputText     = await GitGenerator.ReplaceKeywords(templateData.EditorText, modProject, MainAppData, Data).ConfigureAwait(false);

                                if (!FileCommands.WriteToFile(outputFilePath, outputText))
                                {
                                    Log.Here().Error("[{0}] Failed to create template file at {1}", modProject.ProjectName, templateData.ExportPath);
                                }
                                else
                                {
                                    commitGit      = true;
                                    commitMessage += (", added " + templateData.Name);
                                }
                            }
                            else
                            {
                                Log.Here().Activity("[{0}] Skipping {1}", modProject.ProjectName, templateSetting.TemplateName);
                            }
                        }
                    }
                }

                if (generationSettings.SelectedLicense != LicenseType.None)
                {
                    AppController.Main.UpdateProgress(percentageIncrement, "Generating license...");

                    string outputText = "";
                    if (generationSettings.SelectedLicense == LicenseType.Custom)
                    {
                        var customLicenseTemplate = Data.Templates.Where(t => t.Name == "LICENSE").FirstOrDefault();
                        if (customLicenseTemplate != null)
                        {
                            outputText = customLicenseTemplate.EditorText;
                        }
                    }
                    else
                    {
                        switch (generationSettings.SelectedLicense)
                        {
                        case LicenseType.MIT:
                            outputText = Properties.Resources.License_MIT;
                            break;

                        case LicenseType.Apache:
                            outputText = Properties.Resources.License_Apache;
                            break;

                        case LicenseType.GPL:
                            outputText = Properties.Resources.License_GPL;
                            break;
                        }
                    }

                    if (!String.IsNullOrEmpty(outputText))
                    {
                        outputText = await GitGenerator.ReplaceKeywords(outputText, modProject, MainAppData, Data).ConfigureAwait(false);
                    }

                    string licenseFile = Path.Combine(gitProjectRootDirectory, "LICENSE");

                    if (!FileCommands.WriteToFile(licenseFile, outputText))
                    {
                        Log.Here().Error("[{0}] Failed to write license template file at {1}", modProject.ProjectName, licenseFile);
                    }
                    else
                    {
                        commitGit      = true;
                        commitMessage += ", added license";
                    }
                }

                if (generationSettings.InitGit && commitGit)
                {
                    AppController.Main.UpdateProgress(percentageIncrement, "Committing new files...");
                    var result = await GitGenerator.Commit(gitProjectRootDirectory, commitMessage).ConfigureAwait(false);

                    if (result)
                    {
                        AppController.Main.UpdateProgressLog($"Successfully commited git repo for project {modProject.DisplayName}.");
                    }
                    else
                    {
                        AppController.Main.UpdateProgressLog($"Git repo failed to commit for project {modProject.DisplayName}.");
                    }
                }

                AppController.Main.UpdateProgress(percentageIncrement, "Generating source control data file...");

                SourceControlData sourceControlData = new SourceControlData()
                {
                    ProjectName = modProject.ProjectName,
                    ProjectUUID = modProject.UUID
                };

                modProject.GitData = sourceControlData;

                FileCommands.Save.SaveSourceControlData(sourceControlData, gitProjectRootDirectory);

                AppController.Main.UpdateProgress(percentageIncrement);

                return(true);
            }
            //}
            //catch(Exception ex)
            //{
            //	Log.Here().Error($"Error generating git files: {ex.ToString()}");
            //}
            return(false);
        }