Ejemplo n.º 1
0
        public ExistingSolutionInitVM()
        {
            SolutionPath.PathType         = PathPickerVM.PathTypeOptions.File;
            SolutionPath.ExistCheckOption = PathPickerVM.CheckOptions.On;
            SolutionPath.Filters.Add(new CommonFileDialogFilter("Solution", ".sln"));

            var validation = Observable.CombineLatest(
                SolutionPath.PathState(),
                this.WhenAnyValue(x => x.ProjectName),
                (sln, proj) => (sln, proj, validation: SolutionInitialization.ValidateProjectPath(proj, sln)))
                             .Replay(1)
                             .RefCount();

            _ProjectError = validation
                            .Select(i => (ErrorResponse)i.validation)
                            .ToGuiProperty <ErrorResponse>(this, nameof(ProjectError), ErrorResponse.Success);

            InitializationCall = validation
                                 .Select((i) =>
            {
                if (!i.sln.Succeeded)
                {
                    return(i.sln.BubbleFailure <InitializerCall>());
                }
                if (!i.validation.Succeeded)
                {
                    return(i.validation.BubbleFailure <InitializerCall>());
                }
                return(GetResponse <InitializerCall> .Succeed(async(profile) =>
                {
                    var patcher = new SolutionPatcherVM(profile);
                    SolutionInitialization.CreateProject(i.validation.Value, patcher.Profile.Release.ToCategory());
                    SolutionInitialization.AddProjectToSolution(i.sln.Value, i.validation.Value);
                    patcher.SolutionPath.TargetPath = i.sln.Value;
                    patcher.ProjectSubpath = Path.Combine(i.proj, $"{i.proj}.csproj");
                    return patcher.AsEnumerable();
                }));
            });
        }
Ejemplo n.º 2
0
        public NewSolutionInitVM()
        {
            ParentDirPath.PathType         = PathPickerVM.PathTypeOptions.Folder;
            ParentDirPath.ExistCheckOption = PathPickerVM.CheckOptions.On;

            _SolutionPath = Observable.CombineLatest(
                this.ParentDirPath.PathState(),
                this.WhenAnyValue(x => x.SolutionName),
                (parentDir, slnName) =>
            {
                if (string.IsNullOrWhiteSpace(slnName))
                {
                    return(GetResponse <string> .Fail(val: slnName, reason: "Solution needs a name."));
                }

                // Will reevaluate once parent dir is fixed
                if (parentDir.Failed)
                {
                    return(GetResponse <string> .Succeed(value: slnName));
                }
                try
                {
                    var slnPath = Path.Combine(parentDir.Value, slnName);
                    if (File.Exists(slnPath))
                    {
                        return(GetResponse <string> .Fail(val: slnName, reason: $"Target solution folder cannot already exist as a file: {slnPath}"));
                    }
                    if (Directory.Exists(slnPath) &&
                        (Directory.EnumerateFiles(slnPath).Any() ||
                         Directory.EnumerateDirectories(slnPath).Any()))
                    {
                        return(GetResponse <string> .Fail(val: slnName, reason: $"Target solution folder must be empty: {slnPath}"));
                    }
                    return(GetResponse <string> .Succeed(Path.Combine(slnPath, $"{slnName}.sln")));
                }
                catch (ArgumentException)
                {
                    return(GetResponse <string> .Fail(val: slnName, reason: "Improper solution name. Go simpler."));
                }
            })
                            .ToGuiProperty(this, nameof(SolutionPath));

            var validation = Observable.CombineLatest(
                this.ParentDirPath.PathState(),
                this.WhenAnyValue(x => x.SolutionName),
                this.WhenAnyValue(x => x.SolutionPath),
                this.WhenAnyValue(x => x.ProjectName),
                (parentDir, slnName, sln, proj) =>
            {
                // Use solution name if proj empty.
                if (string.IsNullOrWhiteSpace(proj))
                {
                    proj = SolutionNameProcessor(slnName);
                }
                return(parentDir, sln, proj, validation: SolutionInitialization.ValidateProjectPath(proj, sln));
            })
                             .Replay(1)
                             .RefCount();

            _ProjectError = validation
                            .Select(i => (ErrorResponse)i.validation)
                            .ToGuiProperty <ErrorResponse>(this, nameof(ProjectError), ErrorResponse.Success);

            InitializationCall = validation
                                 .Select((i) =>
            {
                if (i.parentDir.Failed)
                {
                    return(i.parentDir.BubbleFailure <InitializerCall>());
                }
                if (i.sln.Failed)
                {
                    return(i.sln.BubbleFailure <InitializerCall>());
                }
                if (i.validation.Failed)
                {
                    return(i.validation.BubbleFailure <InitializerCall>());
                }
                return(GetResponse <InitializerCall> .Succeed(async(profile) =>
                {
                    var patcher = new SolutionPatcherVM(profile);
                    SolutionInitialization.CreateSolutionFile(i.sln.Value);
                    SolutionInitialization.CreateProject(i.validation.Value, patcher.Profile.Release.ToCategory());
                    SolutionInitialization.AddProjectToSolution(i.sln.Value, i.validation.Value);
                    SolutionInitialization.GenerateGitIgnore(Path.GetDirectoryName(i.sln.Value) !);
                    patcher.SolutionPath.TargetPath = i.sln.Value;
                    var projName = Path.GetFileNameWithoutExtension(i.validation.Value);
                    patcher.ProjectSubpath = Path.Combine(projName, $"{projName}.csproj");
                    return patcher.AsEnumerable();
                }));
            });

            _ProjectNameWatermark = this.WhenAnyValue(x => x.SolutionName)
                                    .Select(x => string.IsNullOrWhiteSpace(x) ? "The name of the patcher" : SolutionNameProcessor(x))
                                    .ToGuiProperty <string>(this, nameof(ProjectNameWatermark), string.Empty);
        }
Ejemplo n.º 3
0
        public ExistingProjectInitVM()
        {
            SolutionPath.PathType         = PathPickerVM.PathTypeOptions.File;
            SolutionPath.ExistCheckOption = PathPickerVM.CheckOptions.On;
            SolutionPath.Filters.Add(new CommonFileDialogFilter("Solution", ".sln"));

            AvailableProjects = this.WhenAnyValue(x => x.SolutionPath.TargetPath)
                                .ObserveOn(RxApp.TaskpoolScheduler)
                                .Select(x => Utility.AvailableProjectSubpaths(x))
                                .Select(x => x.AsObservableChangeSet())
                                .Switch()
                                .ObserveOnGui()
                                .ToObservableCollection(this);

            InitializationCall = SelectedProjects.Connect()
                                 .Transform(subPath =>
            {
                if (subPath == null || this.SolutionPath.TargetPath == null)
                {
                    return(string.Empty);
                }
                try
                {
                    return(Path.Combine(Path.GetDirectoryName(SolutionPath.TargetPath) !, subPath));
                }
                catch (Exception)
                {
                    return(string.Empty);
                }
            })
                                 .Transform(path =>
            {
                var pathPicker = new PathPickerVM
                {
                    PathType         = PathPickerVM.PathTypeOptions.File,
                    ExistCheckOption = PathPickerVM.CheckOptions.On,
                    TargetPath       = path
                };
                pathPicker.Filters.Add(new CommonFileDialogFilter("Project", ".csproj"));
                return(pathPicker);
            })
                                 .DisposeMany()
                                 .QueryWhenChanged(q =>
            {
                if (q.Count == 0)
                {
                    return(GetResponse <InitializerCall> .Fail("No projects selected"));
                }
                var err = q
                          .Select(p => p.ErrorState)
                          .Where(e => e.Failed)
                          .And(ErrorResponse.Success)
                          .First();
                if (err.Failed)
                {
                    return(err.BubbleFailure <InitializerCall>());
                }
                return(GetResponse <InitializerCall> .Succeed(async(profile) =>
                {
                    return q.Select(i =>
                    {
                        var patcher = new SolutionPatcherVM(profile);
                        patcher.SolutionPath.TargetPath = SolutionPath.TargetPath;
                        patcher.ProjectSubpath = i.TargetPath.TrimStart($"{Path.GetDirectoryName(SolutionPath.TargetPath)}\\" !);
                        return patcher;
                    });
                }));
            });
        }