-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from xoofx/improve-new
Improve `dotnet-releaser new` command
- Loading branch information
Showing
4 changed files
with
82 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using LibGit2Sharp; | ||
|
||
namespace DotNetReleaser; | ||
|
||
public partial class ReleaserApp | ||
{ | ||
// (1) (2) | ||
// [email protected]:xoofx/dotnet-releaser.git | ||
private static readonly Regex GitUrlRegex = new Regex(@":(.+)/(.+)\.git$"); | ||
|
||
private async Task<bool> CreateConfigurationFile(string? destinationFilePath, string projectFile, string? user, string? repo, bool force) | ||
private async Task<bool> CreateConfigurationFile(string? destinationFilePath, string? projectFile, string? user, string? repo, bool force) | ||
{ | ||
destinationFilePath ??= Path.Combine(Environment.CurrentDirectory, "dotnet-releaser.toml"); | ||
destinationFilePath = Path.Combine(Environment.CurrentDirectory, destinationFilePath); | ||
|
@@ -18,12 +24,67 @@ private async Task<bool> CreateConfigurationFile(string? destinationFilePath, st | |
return false; | ||
} | ||
|
||
// If projectFile is null, try to find a sln or project files | ||
var folder = Path.GetFullPath(Path.GetDirectoryName(destinationFilePath)!); | ||
if (projectFile is null) | ||
{ | ||
projectFile = Directory.GetFiles(folder).FirstOrDefault(x => x.EndsWith(".sln")); | ||
string kind = "Solution"; | ||
if (projectFile is null) | ||
{ | ||
projectFile = Directory.GetFiles(folder).FirstOrDefault(x => x.EndsWith(".csproj") || x.EndsWith(".fsproj") || x.EndsWith(".vbproj")); | ||
kind = "Project"; | ||
if (projectFile is null) | ||
{ | ||
Error($"Unable to find a solution file (.sln) or project files (.csproj, .fsproj, .vbproj) in the current folder `{folder}`"); | ||
return false; | ||
} | ||
} | ||
projectFile = Path.GetFileName(projectFile); | ||
Info($"{kind} file detected: {projectFile}"); | ||
} | ||
|
||
// Try to detect the user/repo | ||
var repositoryPath = Repository.Discover(Path.GetDirectoryName(destinationFilePath)); | ||
if (repositoryPath is not null && user is null && repo is null) | ||
{ | ||
var repository = new Repository(repositoryPath); | ||
foreach (var remote in repository.Network.Remotes) | ||
{ | ||
var url = remote.Url; | ||
if (url.Contains('@')) | ||
{ | ||
var match = GitUrlRegex.Match(url); | ||
if (match.Success) | ||
{ | ||
user = match.Groups[1].Value; | ||
repo = match.Groups[2].Value; | ||
Info($"git user/repo detected: {user}/{repo}"); | ||
break; | ||
} | ||
} | ||
else if (url.StartsWith("http")) | ||
{ | ||
// https://github.com/xoofx/dotnet-releaser.git | ||
var uri = new Uri(url); | ||
var path = uri.PathAndQuery; | ||
user = Path.GetFileName(Path.GetDirectoryName(path)); | ||
repo = Path.GetFileNameWithoutExtension(path); | ||
Info($"git user/repo detected: {user}/{repo}"); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
user ??= "github_user_or_org_here"; | ||
repo ??= "github_repo_here"; | ||
|
||
var configAsText = $@"# configuration file for dotnet-releaser | ||
[msbuild] | ||
project = ""{projectFile.Replace('\\', '/')}"" | ||
[github] | ||
user = ""{user ?? "github_user_or_org_here"}"" | ||
repo = ""{repo ?? "github_repo_here"}"" | ||
user = ""{user}"" | ||
repo = ""{repo}"" | ||
"; | ||
|
||
// Normalize the output | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters