-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Git external sources #5
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace Flamenco.ExternalSources; | ||
|
||
public abstract class ExternalSourceBase | ||
{ | ||
public abstract Task Download(string destinationDirectory); | ||
|
||
public static ExternalSourceBase Create(string descriptorFilePath) | ||
{ | ||
if (!File.Exists(descriptorFilePath)) | ||
{ | ||
throw new FileNotFoundException("Descriptor file not found.", descriptorFilePath); | ||
} | ||
|
||
var fileContentLines = File.ReadAllLines(descriptorFilePath); | ||
var type = fileContentLines.FirstOrDefault(l => l.StartsWith("type"))?.Split("=").Last(); | ||
|
||
if (type is null) | ||
{ | ||
throw new ApplicationException($"{descriptorFilePath} is missing a type."); | ||
} | ||
|
||
switch (type) | ||
{ | ||
case "git": | ||
var repositoryUrl = fileContentLines.FirstOrDefault(l => l.StartsWith("repo"))?.Split('=').Last(); | ||
var branch = fileContentLines.FirstOrDefault(l => l.StartsWith("branch"))?.Split('=').Last(); | ||
var tag = fileContentLines.FirstOrDefault(l => l.StartsWith("tag"))?.Split('=').Last(); | ||
var commit = fileContentLines.FirstOrDefault(l => l.StartsWith("commit"))?.Split('=').Last(); | ||
|
||
if (repositoryUrl is null) | ||
{ | ||
throw new ApplicationException($"Repository URL is required when descriptor file is git."); | ||
} | ||
|
||
// Reference precedence is commit, then tag, then branch. If all of them are null, the default | ||
// behavior is to check out the repo's default branch. | ||
return new GitExternalSource(new Uri(repositoryUrl), commit ?? (tag ?? branch)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Why do you differentiate between commit, tag and branch? For git all three are a commit-ish that git can resolve. Especially, because there is no logic that would block someone from specifying a branch-name as a tag or commit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For readability's sake. At the end of the day, whatever you input will end up in the same We can, however, change the property name to something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would favor using the git terminology |
||
default: | ||
throw new ApplicationException($"Unknown external source type: {type}."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using LibGit2Sharp; | ||
|
||
namespace Flamenco.ExternalSources; | ||
|
||
public class GitExternalSource(Uri repositoryUrl, string? reference = null) : ExternalSourceBase | ||
{ | ||
public Uri RepositoryUrl { get; } = repositoryUrl; | ||
public string? Reference { get; } = reference; | ||
|
||
public override async Task Download(string destinationDirectory) | ||
{ | ||
await Task.Run(() => Repository.Clone(RepositoryUrl.ToString(), destinationDirectory)); | ||
|
||
if (Reference is not null) | ||
{ | ||
using var repo = new Repository(destinationDirectory); | ||
LibGit2Sharp.Commands.Checkout(repo, Reference); | ||
} | ||
|
||
Directory.Delete(Path.Join(destinationDirectory, ".git"), recursive: true); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Why do you use a custom file format instead of established formats like json, yaml or even xml?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an INI-style file. I aimed at a less verbose, to-the-point, and easily parse-able format. JSON is an option, although INI is even less verbose than that. But I don't think YAML or even XML are suitable, due to their unfriendliness.