-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCreateConfigFileTask.cs
95 lines (85 loc) · 3.86 KB
/
CreateConfigFileTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// -----------------------------------------------------------------------
// <copyright file="CreateConfigFileTask.cs" company="Petabridge, LLC">
// Copyright (C) 2015 - 2024 Petabridge, LLC <https://petabridge.com>
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Incrementalist.Caching;
using Incrementalist.Cmd.Config;
using Microsoft.Extensions.Logging;
namespace Incrementalist.Cmd.Commands
{
/// <summary>
/// Command to create a configuration file from the current options
/// </summary>
public sealed class CreateConfigFileTask
{
private readonly SlnOptions _options;
private readonly ILogger _logger;
public CreateConfigFileTask(SlnOptions options, ILogger logger)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
/// <summary>
/// Creates a configuration file from the current options
/// </summary>
/// <returns>A task representing the asynchronous operation. Returns 0 on success, non-zero on failure.</returns>
public async Task<int> Run()
{
try
{
// Create the config object from the current options
var config = new IncrementalistConfig
{
GitBranch = _options.GitBranch,
SolutionFilePath = _options.SolutionFilePath,
OutputFile = _options.OutputFile,
ListFolders = _options.ListFolders,
WorkingDirectory = _options.WorkingDirectory,
Verbose = _options.Verbose,
TimeoutMinutes = _options.TimeoutMinutes,
ContinueOnError = _options.ContinueOnError,
RunInParallel = _options.RunInParallel,
FailOnNoProjects = _options.FailOnNoProjects,
NoCache = _options.NoCache
};
// Determine the output file path
string configFilePath = _options.ConfigFile;
if (string.IsNullOrEmpty(configFilePath))
{
// Use default filename in the same directory as the dependency cache
var workingDir = _options.WorkingDirectory ?? Directory.GetCurrentDirectory();
var incrementalistDir = Path.Combine(workingDir, IncrementalistFileConstants.IncrementalistDirectory);
// Create the directory if it doesn't exist
if (!Directory.Exists(incrementalistDir))
{
Directory.CreateDirectory(incrementalistDir);
}
configFilePath = Path.Combine(incrementalistDir, IncrementalistConfig.DefaultConfigFileName);
}
// Create the serialization options
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
// Serialize and write the config to file
string json = JsonSerializer.Serialize(config, options);
await File.WriteAllTextAsync(configFilePath, json);
_logger.LogInformation("Configuration file created: {FilePath}", configFilePath);
return 0;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error creating configuration file");
return -1;
}
}
}
}