Update+bugfix
This commit is contained in:
parent
1d49bd6ef3
commit
6ca140cbbd
|
@ -1,4 +1,5 @@
|
|||
@page
|
||||
@using System.Web
|
||||
@model IndexModel
|
||||
@{
|
||||
ViewData["Title"] = "Builds";
|
||||
|
@ -40,7 +41,7 @@
|
|||
}
|
||||
@if (System.IO.File.Exists(System.IO.Path.Combine(Program.Repo._pkgpath, p.Name, "buildlog.txt")))
|
||||
{
|
||||
<td><a role="button" class="btn btn-sm btn-info" target="_blank" href="/Log?package=@p.Name">View Build log</a></td>
|
||||
<td><a role="button" class="btn btn-sm btn-info" target="_blank" href="/Log?package=@HttpUtility.UrlEncode(p.Name)">View Build log</a></td>
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
246
Program.cs
246
Program.cs
|
@ -6,148 +6,130 @@ using System.Threading.Tasks;
|
|||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace repomgr
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static RepoMgr Repo;
|
||||
namespace repomgr {
|
||||
public static class Program {
|
||||
public static RepoMgr Repo;
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
if (File.Exists("/tmp/repomgr.lock") && args[1] != "daemon")
|
||||
{
|
||||
Console.WriteLine("/tmp/repomgr.lock exists, delete it if you are sure no other process is running");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
else if (args[1] != "daemon")
|
||||
{
|
||||
File.Create("/tmp/repomgr.lock");
|
||||
}
|
||||
public static void Main(string[] args) {
|
||||
if (File.Exists("/tmp/repomgr.lock") && args[1] != "daemon") {
|
||||
Console.WriteLine("/tmp/repomgr.lock exists, delete it if you are sure no other process is running");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
else if (args[1] != "daemon") {
|
||||
File.Create("/tmp/repomgr.lock");
|
||||
}
|
||||
|
||||
if (args.Length < 2 || args[1].Equals("help"))
|
||||
PrintHelp();
|
||||
Repo = new RepoMgr(args[0]);
|
||||
if (File.Exists(Path.Combine(args[0], "repomgr.index.json")))
|
||||
Repo.ReadIndex();
|
||||
switch (args[1])
|
||||
{
|
||||
case "daemon":
|
||||
CreateWebHostBuilder(args).Build().Run();
|
||||
break;
|
||||
case "init":
|
||||
if (args.Length != 4)
|
||||
PrintHelp();
|
||||
try
|
||||
{
|
||||
Repo.Init(args[2], args[3]);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Init failed with error: " + e);
|
||||
}
|
||||
if (args.Length < 2 || args[1].Equals("help"))
|
||||
PrintHelp();
|
||||
|
||||
break;
|
||||
case "add":
|
||||
if (args.Length != 3)
|
||||
PrintHelp();
|
||||
try
|
||||
{
|
||||
Repo.Add(args[2]);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Add failed with error: " + e);
|
||||
}
|
||||
Repo = new RepoMgr(args[0]);
|
||||
if (File.Exists(Path.Combine(args[0], "repomgr.index.json")))
|
||||
Repo.ReadIndex();
|
||||
switch (args[1]) {
|
||||
case "daemon":
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
break;
|
||||
case "init":
|
||||
if (args.Length != 4)
|
||||
PrintHelp();
|
||||
try {
|
||||
Repo.Init(args[2], args[3]);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.WriteLine("Init failed with error: " + e);
|
||||
}
|
||||
|
||||
break;
|
||||
case "update":
|
||||
if (args.Length < 3 || args.Length > 4)
|
||||
PrintHelp();
|
||||
try
|
||||
{
|
||||
switch (args.Length)
|
||||
{
|
||||
case 3:
|
||||
Repo.Build(args[2]);
|
||||
break;
|
||||
case 4 when args[3] == "-f":
|
||||
Repo.Build(args[2], true);
|
||||
break;
|
||||
default:
|
||||
PrintHelp();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Build failed with error: " + e);
|
||||
}
|
||||
break;
|
||||
case "add":
|
||||
if (args.Length != 3)
|
||||
PrintHelp();
|
||||
try {
|
||||
Repo.Add(args[2]);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.WriteLine("Add failed with error: " + e);
|
||||
}
|
||||
|
||||
break;
|
||||
case "update-all":
|
||||
if (args.Length != 2)
|
||||
PrintHelp();
|
||||
try
|
||||
{
|
||||
Repo.BuildAll();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("BuildAll failed with error: " + e);
|
||||
}
|
||||
break;
|
||||
case "update":
|
||||
if (args.Length < 3 || args.Length > 4)
|
||||
PrintHelp();
|
||||
try {
|
||||
switch (args.Length) {
|
||||
case 3:
|
||||
Repo.Build(args[2]);
|
||||
break;
|
||||
case 4 when args[3] == "-f":
|
||||
Repo.Build(args[2], true);
|
||||
break;
|
||||
default:
|
||||
PrintHelp();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.WriteLine("Build failed with error: " + e);
|
||||
}
|
||||
|
||||
break;
|
||||
case "remove":
|
||||
if (args.Length != 3)
|
||||
PrintHelp();
|
||||
try
|
||||
{
|
||||
Repo.Remove(args[2]);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Remove failed with error: " + e);
|
||||
}
|
||||
break;
|
||||
case "update-all":
|
||||
if (args.Length != 2)
|
||||
PrintHelp();
|
||||
try {
|
||||
Repo.BuildAll();
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.WriteLine("BuildAll failed with error: " + e);
|
||||
}
|
||||
|
||||
break;
|
||||
case "list":
|
||||
if (args.Length != 2)
|
||||
PrintHelp();
|
||||
try
|
||||
{
|
||||
Repo.List();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("List failed with error " + e);
|
||||
}
|
||||
break;
|
||||
case "remove":
|
||||
if (args.Length != 3)
|
||||
PrintHelp();
|
||||
try {
|
||||
Repo.Remove(args[2]);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.WriteLine("Remove failed with error: " + e);
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
PrintHelp();
|
||||
break;
|
||||
}
|
||||
if (File.Exists("/tmp/repomgr.lock"))
|
||||
File.Delete("/tmp/repomgr.lock");
|
||||
}
|
||||
break;
|
||||
case "list":
|
||||
if (args.Length != 2)
|
||||
PrintHelp();
|
||||
try {
|
||||
Repo.List();
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.WriteLine("List failed with error " + e);
|
||||
}
|
||||
|
||||
private static void PrintHelp()
|
||||
{
|
||||
//TODO: add/remove <package> [...]
|
||||
Console.WriteLine("Usage:");
|
||||
Console.WriteLine("repomgr <data-path> init <repo-path> <reponame>");
|
||||
Console.WriteLine("repomgr <data-path> list");
|
||||
Console.WriteLine("repomgr <data-path> add <package>");
|
||||
Console.WriteLine("repomgr <data-path> remove <package>");
|
||||
Console.WriteLine("repomgr <data-path> update <package> [-f]");
|
||||
Console.WriteLine("repomgr <data-path> update-all");
|
||||
Console.WriteLine("repomgr <data-path> daemon");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
PrintHelp();
|
||||
break;
|
||||
}
|
||||
|
||||
private static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseStartup<Startup>();
|
||||
}
|
||||
if (File.Exists("/tmp/repomgr.lock"))
|
||||
File.Delete("/tmp/repomgr.lock");
|
||||
}
|
||||
|
||||
private static void PrintHelp() {
|
||||
//TODO: add/remove <package> [...]
|
||||
Console.WriteLine("Usage:");
|
||||
Console.WriteLine("repomgr <data-path> init <repo-path> <reponame>");
|
||||
Console.WriteLine("repomgr <data-path> list");
|
||||
Console.WriteLine("repomgr <data-path> add <package>");
|
||||
Console.WriteLine("repomgr <data-path> remove <package>");
|
||||
Console.WriteLine("repomgr <data-path> update <package> [-f]");
|
||||
Console.WriteLine("repomgr <data-path> update-all");
|
||||
Console.WriteLine("repomgr <data-path> daemon");
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
|
||||
}
|
||||
}
|
622
RepoMgr.cs
622
RepoMgr.cs
|
@ -6,343 +6,309 @@ using System.Linq;
|
|||
using LibGit2Sharp;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace repomgr
|
||||
{
|
||||
public class RepoMgr
|
||||
{
|
||||
public RepoMgr(string buildpath)
|
||||
{
|
||||
_buildpath = Path.GetFullPath(buildpath);
|
||||
_pkgpath = Path.Combine(_buildpath, "pkg");
|
||||
}
|
||||
namespace repomgr {
|
||||
public class RepoMgr {
|
||||
public RepoMgr(string buildpath) {
|
||||
_buildpath = Path.GetFullPath(buildpath);
|
||||
_pkgpath = Path.Combine(_buildpath, "pkg");
|
||||
}
|
||||
|
||||
public readonly string _buildpath;
|
||||
public readonly string _pkgpath;
|
||||
public Repository _repo;
|
||||
public void Init(string repopath, string reponame)
|
||||
{
|
||||
_repo = new Repository(repopath, reponame);
|
||||
if (!Directory.Exists(repopath))
|
||||
Directory.CreateDirectory(repopath);
|
||||
WriteIndex();
|
||||
Console.WriteLine("Initialized.");
|
||||
}
|
||||
public void Add(string package)
|
||||
{
|
||||
Package pkg;
|
||||
if (Directory.Exists(Path.Combine(_pkgpath, package)))
|
||||
{
|
||||
if (_repo.Packages.Find(p => p.Name.Equals(package)) != null)
|
||||
{
|
||||
Console.WriteLine("Package already exists.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CheckPackage(package))
|
||||
{
|
||||
throw new Exception("Package directory exists but is invalid (PKGBUILD or .git missing)");
|
||||
}
|
||||
pkg = new Package(package);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_repo.Packages.Find(p => p.Name.Equals(package)) != null)
|
||||
_repo.Packages.RemoveAll(p => p.Name.Equals(package));
|
||||
try
|
||||
{
|
||||
var refcount = LibGit2Sharp.Repository.ListRemoteReferences($"https://aur.archlinux.org/{package}.git").Count();
|
||||
if (refcount < 1)
|
||||
{
|
||||
throw new Exception("git clone failed: Package doesn't exist in remote");
|
||||
}
|
||||
public readonly string _buildpath;
|
||||
public readonly string _pkgpath;
|
||||
public Repository _repo;
|
||||
|
||||
LibGit2Sharp.Repository.Clone($"https://aur.archlinux.org/{package}.git",
|
||||
Path.Combine(_pkgpath, package));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"Error during clone: {e}");
|
||||
}
|
||||
|
||||
pkg = new Package(package);
|
||||
}
|
||||
|
||||
Console.WriteLine($"Adding package {package}...");
|
||||
_repo.Packages.Add(pkg);
|
||||
WriteIndex();
|
||||
}
|
||||
private void Build(Package package, bool force = false)
|
||||
{
|
||||
if (UpdateAvailable(package))
|
||||
UpdatePackage(package);
|
||||
if (File.ReadAllText(Path.Combine(_pkgpath, package.Name, "PKGBUILD")).Contains("pkgver()"))
|
||||
Shell.Exec("makepkg -os --noconfirm", Path.Combine(_pkgpath, package.Name));
|
||||
package.CurrentVersion = Shell.ExecR("source PKGBUILD; echo \"$pkgver-$pkgrel\"", Path.Combine(_pkgpath, package.Name));
|
||||
WriteIndex();
|
||||
if (force)
|
||||
Shell.Exec("makepkg -Ccsf --sign --noconfirm 2>&1| tee buildlog.txt", Path.Combine(_pkgpath, package.Name));
|
||||
else if (package.CurrentVersion != package.RepoVersion)
|
||||
Shell.Exec("makepkg -Ccs --sign --noconfirm 2>&1| tee buildlog.txt", Path.Combine(_pkgpath, package.Name));
|
||||
else return;
|
||||
public void Init(string repopath, string reponame) {
|
||||
_repo = new Repository(repopath, reponame);
|
||||
if (!Directory.Exists(repopath))
|
||||
Directory.CreateDirectory(repopath);
|
||||
WriteIndex();
|
||||
Console.WriteLine("Initialized.");
|
||||
}
|
||||
|
||||
var resultingPackages = Directory
|
||||
.GetFiles(Path.Combine(_pkgpath, package.Name), "*.pkg.tar*");
|
||||
if (resultingPackages.Length < 1)
|
||||
{
|
||||
package.LastBuildSucceeded = false;
|
||||
throw new Exception("makepkg didn't build any package");
|
||||
}
|
||||
public void Add(string package) {
|
||||
Package pkg;
|
||||
if (Directory.Exists(Path.Combine(_pkgpath, package))) {
|
||||
if (_repo.Packages.Find(p => p.Name.Equals(package)) != null) {
|
||||
Console.WriteLine("Package already exists.");
|
||||
return;
|
||||
}
|
||||
|
||||
package.LastBuildSucceeded = true;
|
||||
if (package.PkgFiles.Any())
|
||||
{
|
||||
foreach (var pkgFile in package.PkgFiles)
|
||||
if (File.Exists(Path.Combine(_repo.Path, pkgFile)))
|
||||
File.Delete(Path.Combine(_repo.Path, pkgFile));
|
||||
package.PkgFiles.Clear();
|
||||
}
|
||||
|
||||
foreach (var resultingPackage in resultingPackages.Where(p => p.EndsWith(".sig")))
|
||||
{
|
||||
File.Copy(resultingPackage, Path.Combine(_repo.Path, Path.GetFileName(resultingPackage)), true);
|
||||
File.Delete(resultingPackage);
|
||||
package.PkgFiles.Add(Path.GetFileName(resultingPackage));
|
||||
}
|
||||
|
||||
foreach (var resultingPackage in resultingPackages.Where(p => !p.EndsWith(".sig")))
|
||||
{
|
||||
File.Copy(resultingPackage, Path.Combine(_repo.Path, Path.GetFileName(resultingPackage)), true);
|
||||
File.Delete(resultingPackage);
|
||||
package.PkgFiles.Add(Path.GetFileName(resultingPackage));
|
||||
Shell.Exec($"repo-add --remove --sign {_repo.Name}.db.tar.gz {Path.GetFileName(resultingPackage)}", _repo.Path);
|
||||
}
|
||||
package.RepoVersion = package.CurrentVersion;
|
||||
WriteIndex();
|
||||
}
|
||||
private void Remove(Package package)
|
||||
{
|
||||
var packageDir = Path.Combine(_pkgpath, package.Name);
|
||||
if (Directory.Exists(packageDir))
|
||||
Directory.Delete(packageDir, true);
|
||||
Shell.Exec($"repo-remove --sign {_repo.Name}.db.tar.gz {package.Name}", _repo.Path);
|
||||
if (package.PkgFiles.Any())
|
||||
{
|
||||
foreach (var pkgFile in package.PkgFiles)
|
||||
if (File.Exists(Path.Combine(_repo.Path, pkgFile)))
|
||||
File.Delete(Path.Combine(_repo.Path, pkgFile));
|
||||
package.PkgFiles.Clear();
|
||||
}
|
||||
_repo.Packages.Remove(package);
|
||||
WriteIndex();
|
||||
}
|
||||
public void List()
|
||||
{
|
||||
Console.WriteLine(Shell.Yellow($"{Shell.Bold(_repo.Name)} ({_repo.Packages.Count} packages):"));
|
||||
foreach (var package in _repo.Packages.OrderBy(p => p.Name))
|
||||
{
|
||||
var line = $"{Shell.Bold(package.Name)}";
|
||||
if (package.RepoVersion != package.CurrentVersion)
|
||||
line += $" ({Shell.Red(package.RepoVersion)} {Shell.Gray("->")} {Shell.Yellow(package.CurrentVersion)})";
|
||||
else
|
||||
line += $" ({Shell.Green(package.RepoVersion)})";
|
||||
if (package.LastBuildSucceeded)
|
||||
line += $" [{Shell.Green(Shell.Bold("BUILD PASSING"))}]";
|
||||
else
|
||||
line += $" [{Shell.Red(Shell.Bold("BUILD FAILING"))}]";
|
||||
Console.WriteLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
// util stuff
|
||||
private Package GetPackage(string package)
|
||||
{
|
||||
var pkg = _repo.Packages.FirstOrDefault(p => p.Name.Equals(package));
|
||||
if (pkg != null) return pkg;
|
||||
throw new Exception("Package not found.");
|
||||
}
|
||||
// git fetch, returns true if differences between origin/master and master found
|
||||
private bool UpdateAvailable(Package package)
|
||||
{
|
||||
var repo = new LibGit2Sharp.Repository(Path.Combine(_pkgpath, package.Name));
|
||||
var remote = repo.Network.Remotes["origin"];
|
||||
var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
|
||||
Commands.Fetch(repo, remote.Name, refSpecs, null, null);
|
||||
|
||||
return repo.Diff.Compare<TreeChanges>(repo.Branches["origin/master"].Tip.Tree,
|
||||
DiffTargets.Index | DiffTargets.WorkingDirectory).Any();
|
||||
}
|
||||
// resets master to origin/master
|
||||
private void UpdatePackage(Package package)
|
||||
{
|
||||
var repo = new LibGit2Sharp.Repository(Path.Combine(_pkgpath, package.Name));
|
||||
var originMaster = repo.Branches["origin/master"];
|
||||
repo.Reset(ResetMode.Hard, originMaster.Tip);
|
||||
}
|
||||
public void ReadIndex()
|
||||
{
|
||||
try
|
||||
{
|
||||
_repo = JsonConvert.DeserializeObject<Repository>(
|
||||
File.ReadAllText(Path.Combine(_buildpath, "repomgr.index.json")));
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
throw new Exception("configuration file not found or wrong permissions. Did you run repomgr init?");
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
throw new Exception("configuration corrupt. Please check manually or re-init repo.");
|
||||
}
|
||||
}
|
||||
private void WriteIndex()
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(_repo);
|
||||
if (!Directory.Exists(_buildpath))
|
||||
Directory.CreateDirectory(_buildpath);
|
||||
File.WriteAllText(Path.Combine(_buildpath, "repomgr.index.json"), json);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
throw new Exception("Unable to write configuration. Check permissions.");
|
||||
}
|
||||
}
|
||||
private bool CheckPackage(string package)
|
||||
{
|
||||
return Directory.Exists(Path.Combine(_pkgpath, package, ".git")) &&
|
||||
File.Exists(Path.Combine(_pkgpath, package, "PKGBUILD"));
|
||||
}
|
||||
public void Build(string package, bool force = false)
|
||||
{
|
||||
if (!CheckPackage(package)) return;
|
||||
var iPackage = GetPackage(package);
|
||||
try
|
||||
{
|
||||
Build(iPackage, force);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
iPackage.LastBuildSucceeded = false;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void BuildAll()
|
||||
{
|
||||
foreach (var package in _repo.Packages)
|
||||
{
|
||||
if (!CheckPackage(package.Name)) return;
|
||||
try
|
||||
{
|
||||
Build(package);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
package.LastBuildSucceeded = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Remove(string package)
|
||||
{
|
||||
if (CheckPackage(package))
|
||||
Remove(GetPackage(package));
|
||||
}
|
||||
}
|
||||
if (!CheckPackage(package)) {
|
||||
throw new Exception("Package directory exists but is invalid (PKGBUILD or .git missing)");
|
||||
}
|
||||
|
||||
public class Repository
|
||||
{
|
||||
public Repository(string path, string name)
|
||||
{
|
||||
Path = path;
|
||||
Name = name;
|
||||
Packages = new List<Package>();
|
||||
}
|
||||
pkg = new Package(package);
|
||||
}
|
||||
else {
|
||||
if (_repo.Packages.Find(p => p.Name.Equals(package)) != null)
|
||||
_repo.Packages.RemoveAll(p => p.Name.Equals(package));
|
||||
try {
|
||||
var refcount = LibGit2Sharp.Repository.ListRemoteReferences($"https://aur.archlinux.org/{package}.git").Count();
|
||||
if (refcount < 1) {
|
||||
throw new Exception("git clone failed: Package doesn't exist in remote");
|
||||
}
|
||||
|
||||
public readonly string Path;
|
||||
public readonly string Name;
|
||||
public readonly List<Package> Packages;
|
||||
}
|
||||
LibGit2Sharp.Repository.Clone($"https://aur.archlinux.org/{package}.git", Path.Combine(_pkgpath, package));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new Exception($"Error during clone: {e}");
|
||||
}
|
||||
|
||||
public class Package
|
||||
{
|
||||
public Package(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
pkg = new Package(package);
|
||||
}
|
||||
|
||||
public readonly string Name;
|
||||
public string CurrentVersion = "never-updated";
|
||||
public string RepoVersion = "nA";
|
||||
public bool LastBuildSucceeded = true;
|
||||
public readonly List<string> PkgFiles = new List<string>();
|
||||
}
|
||||
Console.WriteLine($"Adding package {package}...");
|
||||
_repo.Packages.Add(pkg);
|
||||
WriteIndex();
|
||||
}
|
||||
|
||||
internal static class Shell
|
||||
{
|
||||
public static void Exec(string cmd, string workingDirectory)
|
||||
{
|
||||
var escapedArgs = cmd.Replace("\"", "\\\"");
|
||||
|
||||
var process = new Process()
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "/bin/bash",
|
||||
Arguments = $"-c \"{escapedArgs}\"",
|
||||
WorkingDirectory = workingDirectory,
|
||||
RedirectStandardOutput = false,
|
||||
RedirectStandardError = false,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
process.WaitForExit();
|
||||
}
|
||||
|
||||
public static string ExecR(string cmd, string workingDirectory)
|
||||
{
|
||||
var escapedArgs = cmd.Replace("\"", "\\\"");
|
||||
|
||||
var process = new Process()
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "/bin/bash",
|
||||
Arguments = $"-c \"{escapedArgs}\"",
|
||||
WorkingDirectory = workingDirectory,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
var stdout = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
return stdout.Trim();
|
||||
}
|
||||
private void Build(Package package, bool force = false) {
|
||||
if (UpdateAvailable(package))
|
||||
UpdatePackage(package);
|
||||
if (File.ReadAllText(Path.Combine(_pkgpath, package.Name, "PKGBUILD")).Contains("pkgver()"))
|
||||
Shell.Exec("makepkg -os --noconfirm", Path.Combine(_pkgpath, package.Name));
|
||||
package.CurrentVersion = Shell.ExecR("source PKGBUILD; echo \"$pkgver-$pkgrel\"", Path.Combine(_pkgpath, package.Name));
|
||||
WriteIndex();
|
||||
if (force)
|
||||
Shell.Exec("makepkg -Ccsf --sign --noconfirm 2>&1| tee buildlog.txt", Path.Combine(_pkgpath, package.Name));
|
||||
else if (package.CurrentVersion != package.RepoVersion)
|
||||
Shell.Exec("makepkg -Ccs --sign --noconfirm 2>&1| tee buildlog.txt", Path.Combine(_pkgpath, package.Name));
|
||||
else
|
||||
return;
|
||||
|
||||
public static string Bold(string s)
|
||||
{
|
||||
return $"\x1B[1m{s}\x1B[21m";
|
||||
}
|
||||
var resultingPackages = Directory.GetFiles(Path.Combine(_pkgpath, package.Name), "*.pkg.tar*");
|
||||
if (resultingPackages.Length < 1) {
|
||||
package.LastBuildSucceeded = false;
|
||||
throw new Exception("makepkg didn't build any package");
|
||||
}
|
||||
|
||||
public static string Red(string s)
|
||||
{
|
||||
return $"\x1b[31m{s}\x1b[39m";
|
||||
}
|
||||
public static string Yellow(string s)
|
||||
{
|
||||
return $"\x1b[33m{s}\x1b[39m";
|
||||
}
|
||||
public static string Green(string s)
|
||||
{
|
||||
return $"\x1b[32m{s}\x1b[39m";
|
||||
}
|
||||
public static string Gray(string s)
|
||||
{
|
||||
return $"\x1b[90m{s}\x1b[39m";
|
||||
}
|
||||
}
|
||||
package.LastBuildSucceeded = true;
|
||||
if (package.PkgFiles.Any()) {
|
||||
foreach (var pkgFile in package.PkgFiles)
|
||||
if (File.Exists(Path.Combine(_repo.Path, pkgFile)))
|
||||
File.Delete(Path.Combine(_repo.Path, pkgFile));
|
||||
package.PkgFiles.Clear();
|
||||
}
|
||||
|
||||
foreach (var resultingPackage in resultingPackages.Where(p => p.EndsWith(".sig"))) {
|
||||
File.Copy(resultingPackage, Path.Combine(_repo.Path, Path.GetFileName(resultingPackage)), true);
|
||||
File.Delete(resultingPackage);
|
||||
package.PkgFiles.Add(Path.GetFileName(resultingPackage));
|
||||
}
|
||||
|
||||
foreach (var resultingPackage in resultingPackages.Where(p => !p.EndsWith(".sig"))) {
|
||||
File.Copy(resultingPackage, Path.Combine(_repo.Path, Path.GetFileName(resultingPackage)), true);
|
||||
File.Delete(resultingPackage);
|
||||
package.PkgFiles.Add(Path.GetFileName(resultingPackage));
|
||||
Shell.Exec($"repo-add --remove --sign {_repo.Name}.db.tar.gz {Path.GetFileName(resultingPackage)}", _repo.Path);
|
||||
}
|
||||
|
||||
package.RepoVersion = package.CurrentVersion;
|
||||
WriteIndex();
|
||||
}
|
||||
|
||||
private void Remove(Package package) {
|
||||
var packageDir = Path.Combine(_pkgpath, package.Name);
|
||||
if (Directory.Exists(packageDir))
|
||||
Directory.Delete(packageDir, true);
|
||||
Shell.Exec($"repo-remove --sign {_repo.Name}.db.tar.gz {package.Name}", _repo.Path);
|
||||
if (package.PkgFiles.Any()) {
|
||||
foreach (var pkgFile in package.PkgFiles)
|
||||
if (File.Exists(Path.Combine(_repo.Path, pkgFile)))
|
||||
File.Delete(Path.Combine(_repo.Path, pkgFile));
|
||||
package.PkgFiles.Clear();
|
||||
}
|
||||
|
||||
_repo.Packages.Remove(package);
|
||||
WriteIndex();
|
||||
}
|
||||
|
||||
public void List() {
|
||||
Console.WriteLine(Shell.Yellow($"{Shell.Bold(_repo.Name)} ({_repo.Packages.Count} packages):"));
|
||||
foreach (var package in _repo.Packages.OrderBy(p => p.Name)) {
|
||||
var line = $"{Shell.Bold(package.Name)}";
|
||||
if (package.RepoVersion != package.CurrentVersion)
|
||||
line += $" ({Shell.Red(package.RepoVersion)} {Shell.Gray("->")} {Shell.Yellow(package.CurrentVersion)})";
|
||||
else
|
||||
line += $" ({Shell.Green(package.RepoVersion)})";
|
||||
if (package.LastBuildSucceeded)
|
||||
line += $" [{Shell.Green(Shell.Bold("BUILD PASSING"))}]";
|
||||
else
|
||||
line += $" [{Shell.Red(Shell.Bold("BUILD FAILING"))}]";
|
||||
Console.WriteLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
// util stuff
|
||||
private Package GetPackage(string package) {
|
||||
var pkg = _repo.Packages.FirstOrDefault(p => p.Name.Equals(package));
|
||||
if (pkg != null)
|
||||
return pkg;
|
||||
|
||||
throw new Exception("Package not found.");
|
||||
}
|
||||
|
||||
// git fetch, returns true if differences between origin/master and master found
|
||||
private bool UpdateAvailable(Package package) {
|
||||
var repo = new LibGit2Sharp.Repository(Path.Combine(_pkgpath, package.Name));
|
||||
var remote = repo.Network.Remotes["origin"];
|
||||
var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
|
||||
Commands.Fetch(repo, remote.Name, refSpecs, null, null);
|
||||
|
||||
return repo.Diff.Compare<TreeChanges>(repo.Branches["origin/master"].Tip.Tree, DiffTargets.Index | DiffTargets.WorkingDirectory).Any();
|
||||
}
|
||||
|
||||
// resets master to origin/master
|
||||
private void UpdatePackage(Package package) {
|
||||
var repo = new LibGit2Sharp.Repository(Path.Combine(_pkgpath, package.Name));
|
||||
var originMaster = repo.Branches["origin/master"];
|
||||
repo.Reset(ResetMode.Hard, originMaster.Tip);
|
||||
repo.RemoveUntrackedFiles();
|
||||
}
|
||||
|
||||
public void ReadIndex() {
|
||||
try {
|
||||
_repo = JsonConvert.DeserializeObject<Repository>(File.ReadAllText(Path.Combine(_buildpath, "repomgr.index.json")));
|
||||
}
|
||||
catch (IOException) {
|
||||
throw new Exception("configuration file not found or wrong permissions. Did you run repomgr init?");
|
||||
}
|
||||
catch (JsonException) {
|
||||
throw new Exception("configuration corrupt. Please check manually or re-init repo.");
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteIndex() {
|
||||
try {
|
||||
var json = JsonConvert.SerializeObject(_repo);
|
||||
if (!Directory.Exists(_buildpath))
|
||||
Directory.CreateDirectory(_buildpath);
|
||||
File.WriteAllText(Path.Combine(_buildpath, "repomgr.index.json"), json);
|
||||
}
|
||||
catch (IOException) {
|
||||
throw new Exception("Unable to write configuration. Check permissions.");
|
||||
}
|
||||
}
|
||||
|
||||
private bool CheckPackage(string package) {
|
||||
return Directory.Exists(Path.Combine(_pkgpath, package, ".git")) && File.Exists(Path.Combine(_pkgpath, package, "PKGBUILD"));
|
||||
}
|
||||
|
||||
public void Build(string package, bool force = false) {
|
||||
if (!CheckPackage(package))
|
||||
return;
|
||||
|
||||
var iPackage = GetPackage(package);
|
||||
try {
|
||||
Build(iPackage, force);
|
||||
}
|
||||
catch (Exception) {
|
||||
iPackage.LastBuildSucceeded = false;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void BuildAll() {
|
||||
foreach (var package in _repo.Packages) {
|
||||
if (!CheckPackage(package.Name))
|
||||
return;
|
||||
|
||||
try {
|
||||
Build(package);
|
||||
}
|
||||
catch (Exception) {
|
||||
package.LastBuildSucceeded = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove(string package) {
|
||||
if (CheckPackage(package))
|
||||
Remove(GetPackage(package));
|
||||
}
|
||||
}
|
||||
|
||||
public class Repository {
|
||||
public Repository(string path, string name) {
|
||||
Path = path;
|
||||
Name = name;
|
||||
Packages = new List<Package>();
|
||||
}
|
||||
|
||||
public readonly string Path;
|
||||
public readonly string Name;
|
||||
public readonly List<Package> Packages;
|
||||
}
|
||||
|
||||
public class Package {
|
||||
public Package(string name) {
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public readonly string Name;
|
||||
public string CurrentVersion = "never-updated";
|
||||
public string RepoVersion = "nA";
|
||||
public bool LastBuildSucceeded = true;
|
||||
public readonly List<string> PkgFiles = new List<string>();
|
||||
}
|
||||
|
||||
internal static class Shell {
|
||||
public static void Exec(string cmd, string workingDirectory) {
|
||||
var escapedArgs = cmd.Replace("\"", "\\\"");
|
||||
|
||||
var process = new Process() {
|
||||
StartInfo = new ProcessStartInfo {
|
||||
FileName = "/bin/bash",
|
||||
Arguments = $"-c \"{escapedArgs}\"",
|
||||
WorkingDirectory = workingDirectory,
|
||||
RedirectStandardOutput = false,
|
||||
RedirectStandardError = false,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
process.WaitForExit();
|
||||
}
|
||||
|
||||
public static string ExecR(string cmd, string workingDirectory) {
|
||||
var escapedArgs = cmd.Replace("\"", "\\\"");
|
||||
|
||||
var process = new Process() {
|
||||
StartInfo = new ProcessStartInfo {
|
||||
FileName = "/bin/bash",
|
||||
Arguments = $"-c \"{escapedArgs}\"",
|
||||
WorkingDirectory = workingDirectory,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
var stdout = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
return stdout.Trim();
|
||||
}
|
||||
|
||||
public static string Bold(string s) {
|
||||
return $"\x1B[1m{s}\x1B[0m";
|
||||
}
|
||||
|
||||
public static string Red(string s) {
|
||||
return $"\x1b[31m{s}\x1b[39m";
|
||||
}
|
||||
|
||||
public static string Yellow(string s) {
|
||||
return $"\x1b[33m{s}\x1b[39m";
|
||||
}
|
||||
|
||||
public static string Green(string s) {
|
||||
return $"\x1b[32m{s}\x1b[39m";
|
||||
}
|
||||
|
||||
public static string Gray(string s) {
|
||||
return $"\x1b[90m{s}\x1b[39m";
|
||||
}
|
||||
}
|
||||
}
|
49
Startup.cs
49
Startup.cs
|
@ -9,51 +9,48 @@ using Microsoft.AspNetCore.HttpsPolicy;
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace repomgr
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
public class Startup {
|
||||
public Startup(IConfiguration configuration) => Configuration = configuration;
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.Configure<CookiePolicyOptions>(options =>
|
||||
{
|
||||
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
|
||||
options.CheckConsentNeeded = context => true;
|
||||
options.MinimumSameSitePolicy = SameSiteMode.None;
|
||||
});
|
||||
|
||||
|
||||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
||||
public void ConfigureServices(IServiceCollection services) {
|
||||
services.AddRazorPages();
|
||||
#if (DEBUG)
|
||||
services.AddControllers().AddRazorRuntimeCompilation();
|
||||
#else
|
||||
services.AddControllers();
|
||||
#endif
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
|
||||
if (env.IsDevelopment()) {
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
app.UseExceptionHandler("/Error");
|
||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseStaticFiles();
|
||||
app.UseCookiePolicy();
|
||||
|
||||
app.UseMvc();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints => {
|
||||
endpoints.MapRazorPages();
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,21 +1,21 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<DebugType>full</DebugType>
|
||||
<Configurations>Debug;Release</Configurations>
|
||||
<Platforms>AnyCPU</Platforms>
|
||||
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
|
||||
<PublishSingleFile>true</PublishSingleFile>
|
||||
<OutputType>Exe</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LibGit2Sharp" Version="0.26.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
|
||||
<PackageReference Include="LibGit2Sharp" Version="0.26.2" />
|
||||
<PackageReference Include="LibGit2Sharp.NativeBinaries" Version="2.0.306" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.5" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
|
||||
<PackageReference Include="LibGit2Sharp.NativeBinaries" Version="2.0.306" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
Loading…
Reference in a new issue