diff --git a/.github/workflows/duplicate_samples.yaml b/.github/workflows/duplicate_samples.yaml index b650ae5d0..e24841e5e 100644 --- a/.github/workflows/duplicate_samples.yaml +++ b/.github/workflows/duplicate_samples.yaml @@ -3,25 +3,12 @@ name: Duplicate Samples 🪞 on: workflow_call: workflow_dispatch: - inputs: - arguments: - required: false - description: 'Run Arguments' - type: string - configuration: - required: true - description: 'Run Configuration' - default: 'Release' - type: choice - options: - - Release - - Debug jobs: setup: name: Duplicate Samples 🪞 uses: ChainSafe/web3.unity/.github/workflows/setup.yaml@main with: - arguments: "-duplicate_samples ${{ github.event.inputs.arguments || '-git:enabled' }} -c ${{ github.event.inputs.configuration || 'Release' }}" + arguments: "-d" secrets: inherit \ No newline at end of file diff --git a/.github/workflows/pull_request_checks_dev.yml b/.github/workflows/pull_request_checks_dev.yml index 627e50c9c..2463f30c2 100644 --- a/.github/workflows/pull_request_checks_dev.yml +++ b/.github/workflows/pull_request_checks_dev.yml @@ -9,14 +9,21 @@ on: - ready_for_review jobs: + pre_job: + name: Pre Job 🚀 + runs-on: ubuntu-latest + if: ${{ github.event.action == 'ready_for_review' || (!github.event.pull_request.draft && contains( github.event.pull_request.labels.*.name, 'ready-to-merge')) }} + steps: + - run: echo "Proceeding to checks..." web3_tests: name: Web3 tests 🕸 - if: ${{ github.event.action == 'ready_for_review' || github.event.label.name == 'ready-to-merge'}} uses: ChainSafe/web3.unity/.github/workflows/test.yaml@main + needs: [ pre_job ] analyze_code: name: Analyze 🧐 - if: ${{ github.event.action == 'ready_for_review' || github.event.label.name == 'ready-to-merge'}} uses: ChainSafe/web3.unity/.github/workflows/codeql.yml@main + needs: [ pre_job ] documentation_check: name: Documentation Check 📚 - uses: ChainSafe/web3.unity/.github/workflows/documentation_check.yml@main \ No newline at end of file + uses: ChainSafe/web3.unity/.github/workflows/documentation_check.yml@main + needs: [ pre_job ] \ No newline at end of file diff --git a/.github/workflows/push_checks_main.yml b/.github/workflows/push_checks_main.yml index f6aebda25..025d8c5a3 100644 --- a/.github/workflows/push_checks_main.yml +++ b/.github/workflows/push_checks_main.yml @@ -9,5 +9,7 @@ jobs: name: Sync Branches ♾️ uses: ChainSafe/web3.unity/.github/workflows/sync_branches.yaml@main with: - branch: dev + base: main + target: dev + skip_ci: false secrets: inherit \ No newline at end of file diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index a73bb718c..8cbadce4a 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -15,7 +15,7 @@ jobs: name: Setup job uses: ChainSafe/web3.unity/.github/workflows/setup.yaml@main with: - arguments: "-release:${{ github.event.inputs.version }}" + arguments: "--deploy ${{ github.event.inputs.version }}" secrets: inherit release: name: Release job diff --git a/.github/workflows/setup.yaml b/.github/workflows/setup.yaml index c24b07ef3..b48230a69 100644 --- a/.github/workflows/setup.yaml +++ b/.github/workflows/setup.yaml @@ -6,12 +6,24 @@ on: arguments: required: true type: string + configuration: + required: false + type: string + default: "Release" workflow_dispatch: inputs: arguments: required: true description: 'Arguments to pass to the setup script' type: string + configuration: + required: true + description: 'Run Configuration' + default: 'Release' + type: choice + options: + - Release + - Debug env: git_email: "${{ github.actor }}@users.noreply.github.com" git_actor: "${{ github.actor }}" @@ -28,6 +40,7 @@ jobs: lfs: true ssh-key: ${{ secrets.DEPLOY_KEY }} submodules: recursive + fetch-depth: 100 - name: Setup .NET uses: actions/setup-dotnet@v3 with: @@ -36,10 +49,5 @@ jobs: - name: Run run: | cd Setup - dotnet run ${{ inputs.arguments || github.event.inputs.arguments }} Setup/Setup.csproj - git reset --hard - - name: Cache - uses: actions/cache/save@v4 - with: - path: .git - key: ${{ runner.os }}-git \ No newline at end of file + dotnet run ${{ inputs.arguments || github.event.inputs.arguments }} -c ${{ inputs.configuration || github.event.inputs.configuration }} + git reset --hard \ No newline at end of file diff --git a/.github/workflows/sync_branches.yaml b/.github/workflows/sync_branches.yaml index feaa06195..00110345b 100644 --- a/.github/workflows/sync_branches.yaml +++ b/.github/workflows/sync_branches.yaml @@ -1,39 +1,37 @@ -name: Sync Branches +name: Sync Branches ♾️ on: workflow_call: inputs: - branch: + base: required: true type: string + target: + required: true + type: string + skip_ci: + type: boolean + default: true workflow_dispatch: inputs: - branch: + base: required: true - description: "Branch to Sync" + description: "Base branch to Sync" type: string + target: + required: true + description: "Target branch to Sync" + type: string + skip_ci: + required: true + description: "Should push skip CI" + type: boolean + default: true jobs: setup: name: Sync Branches ♾️ - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - ref: ${{ github.head_ref || github.ref_name }} - lfs: true - ssh-key: ${{ secrets.DEPLOY_KEY }} - - name: Sync Branches - run: | - git config user.email $git_email - git config user.name $git_actor - git fetch origin ${{ github.event.inputs.branch }} - git checkout ${{ github.event.inputs.branch }} - git pull - git merge main --allow-unrelated-histories - git push - shell: bash - env: - git_email: "${{ github.actor }}@users.noreply.github.com" - git_actor: "${{ github.actor }}" \ No newline at end of file + uses: ChainSafe/web3.unity/.github/workflows/setup.yaml@rob/sync-branches-ci-fix + with: + arguments: "sync -b ${{ github.event.inputs.base || inputs.base }} -t ${{ github.event.inputs.target || inputs.target }} -s ${{ github.event.inputs.skip_ci || inputs.skip_ci }}" + secrets: inherit \ No newline at end of file diff --git a/.github/workflows/sync_dependencies.yaml b/.github/workflows/sync_dependencies.yaml index 5e0f0b3fa..500ff9b72 100644 --- a/.github/workflows/sync_dependencies.yaml +++ b/.github/workflows/sync_dependencies.yaml @@ -4,10 +4,6 @@ on: workflow_call: workflow_dispatch: inputs: - arguments: - required: false - description: 'Run Arguments' - type: string configuration: required: true description: 'Run Configuration' @@ -22,6 +18,7 @@ jobs: name: Sync Dependencies 🔄 uses: ChainSafe/web3.unity/.github/workflows/setup.yaml@main with: - arguments: "-sync_dependencies ${{ github.event.inputs.arguments || '-git:enabled' }} -c ${{ github.event.inputs.configuration || 'Release' }}" + arguments: "-s" + configuration: ${{ github.event.inputs.configuration || 'Release' }} secrets: inherit \ No newline at end of file diff --git a/Setup/DefaultOptions.cs b/Setup/DefaultOptions.cs new file mode 100644 index 000000000..76c214d06 --- /dev/null +++ b/Setup/DefaultOptions.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using CommandLine; +using Setup.Utils; + +namespace Setup; + +[Verb("do", true, HelpText = "Runs default options.")] +public class DefaultOptions +{ + [Option('s', "sync_dependencies", Required = false, Default = false, HelpText = "Generate and copy dependencies to Sample Project.")] + public bool SyncDependencies { get; set; } + + [Option('d', "duplicate_samples", Required = false, Default = false, HelpText = "Duplicate samples in Sample Project into package samples.")] + public bool DuplicateSamples { get; set; } + + [Option("deploy", Required = false, HelpText = "Version to release.")] + public string Release { get; set; } + + [Option('g', "git", Required = false, Default = true, HelpText = "Enable Git.")] + public bool? EnableGit { get; set; } + + public List GetRunnableList() + { + List runnableList = new List(); + + if (SyncDependencies) + { + runnableList.Add(new SyncDependencies()); + } + + if (DuplicateSamples) + { + runnableList.Add(new DuplicateSamples()); + } + + if (!string.IsNullOrEmpty(Release)) + { + runnableList.Add(new Release(Release)); + } + + EnableGit ??= false; + + runnableList.Add(new Git(EnableGit.Value)); + + return runnableList; + } +} \ No newline at end of file diff --git a/Setup/Git.cs b/Setup/Git.cs index 1c2db11aa..db447245f 100644 --- a/Setup/Git.cs +++ b/Setup/Git.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using Setup.Utils; namespace Setup; @@ -8,46 +6,63 @@ namespace Setup; /// /// Git helper class. /// -public static class Git +public class Git : IRunnable { private static bool _configured; - public static bool Enabled { get; private set; } = true; + public static bool Enabled { get; private set; } - public static void Configure(string configuration) + public int Order => - 1; + + public Git(bool enabled) + { + Enabled = enabled; + } + + public void Run() + { + Console.WriteLine($"Git {nameof(Enabled)} : {Enabled}"); + + Execute("status"); + } + + private static void Execute(string command) { - switch (configuration) + if (!Enabled) { - case "enabled": - Enabled = true; - break; - case "disabled": - Enabled = false; - break; - default: - throw new Exception($"-git can't configure {configuration}"); + Console.WriteLine($"Git disabled skipping command: {command}"); + } + else + { + if (!_configured) + { + "git config user.email $git_email".Run(); + + "git config user.name $git_actor".Run(); + + _configured = true; + } + + $"git {command}".Run(); } } public static void Add(string path) { - $"git add \"{path}\" -f".Run(); + Execute($"add \"{path}\" -f"); } - public static void Commit(string message, string[] tags = null, bool skipCi = true) + public static void Commit(string message, string[] tags = null, bool skipCi = true, bool allowEmpty = false) { - if (!_configured) - { - Configure(); - } - if (skipCi) { message += " [skip ci]"; } - - // Checks if there are any changes to commit before committing - $"git diff-index --cached --quiet HEAD || git commit -m \"{message}\"".Run(); + + Execute(allowEmpty + ? $"commit --allow-empty -m \"{message}\"" + // Checks if there are any changes to commit before committing + : $"diff-index --cached --quiet HEAD || git commit -m \"{message}\""); if (tags != null) { @@ -63,7 +78,7 @@ public static void Commit(string message, string[] tags = null, bool skipCi = tr public static void Push(string[] tags = null) { - "git push -f".Run(); + Execute("push -f"); if (tags != null) { @@ -71,7 +86,7 @@ public static void Push(string[] tags = null) { if (!string.IsNullOrEmpty(tag)) { - $"git push origin \"{tag}\"".Run(); + Execute($"push origin \"{tag}\""); } } } @@ -83,17 +98,43 @@ public static void CommitAndPush(string message, string[] tags = null, bool skip Push(tags); } + + public static void Fetch(string branch) + { + Execute($"fetch origin {branch}"); + } - public static void Tag(string tag) + public static void Checkout(string branch, string path = "") { - $"git tag \"{tag}\"".Run(); + string command = $"checkout {branch}"; + + if (!string.IsNullOrEmpty(path)) + { + command += $" \"{path}\""; + } + + Execute(command); } - private static void Configure() + public static void Merge(string branch, string message = "", bool allowUnrelatedHistories = true) { - "git config user.email $git_email".Run(); - "git config user.name $git_actor".Run(); + string command = $"merge {branch} --no-edit --commit --no-ff"; - _configured = true; + if (!string.IsNullOrEmpty(message)) + { + command += $" -m \"{message}\""; + } + + if (allowUnrelatedHistories) + { + command += " --allow-unrelated-histories"; + } + + Execute(command); + } + + public static void Tag(string tag) + { + Execute($"tag \"{tag}\""); } } \ No newline at end of file diff --git a/Setup/Setup.cs b/Setup/Setup.cs index 28d96e9d2..aef349527 100644 --- a/Setup/Setup.cs +++ b/Setup/Setup.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using CommandLine; using Newtonsoft.Json; using Setup.Utils; @@ -57,35 +58,20 @@ private static List GetRunnableList(string[] args) { List runnableList = new List(); - // Parse arguments and Run operations based on that. - foreach (var arg in args) - { - switch (arg) + Parser.Default.ParseArguments(args) + .WithParsed(options => { - case not null when arg.StartsWith("-release"): - - string version = arg.Split(":")[1]; - runnableList.AddRunnable(new Release(version)); - break; - - case "-duplicate_samples": - runnableList.AddRunnable(new DuplicateSamples()); - break; - - case "-sync_dependencies": - runnableList.AddRunnable(new SyncDependencies()); - break; - - case not null when arg.StartsWith("-git"): - - string configuration = arg.Split(":")[1]; - Git.Configure(configuration); - continue; - - default: - continue; - } - } + switch (options) + { + case DefaultOptions defaultOptions: + runnableList.AddRange(defaultOptions.GetRunnableList()); + break; + case SyncBranches syncBranches: + runnableList.Add(syncBranches); + runnableList.Add(new Git(true)); + break; + } + }); return runnableList; } diff --git a/Setup/Setup.csproj b/Setup/Setup.csproj index e567b1161..10b9afa2f 100644 --- a/Setup/Setup.csproj +++ b/Setup/Setup.csproj @@ -6,6 +6,7 @@ + diff --git a/Setup/SyncBranches.cs b/Setup/SyncBranches.cs new file mode 100644 index 000000000..d7538a944 --- /dev/null +++ b/Setup/SyncBranches.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CommandLine; +using Newtonsoft.Json; +using Setup.Utils; + +namespace Setup; + +/// +/// Sync branches (merge). +/// +[Verb("sync", HelpText = "Sync branches.")] +public class SyncBranches : IRunnable +{ + public int Order => 0; + + public IEnumerable CommandLineArgument { get; set; } + + private readonly string[] _excludedPaths; + + [Option('b', "base", Required = true, HelpText = "Base branch.")] + public string Base { get; set; } + + [Option('t', "target", Required = true, HelpText = "Target branch.")] + public string Target { get; set; } + + [Option('s', "skip_ci", Required = false, Default = true, HelpText = "Disable/Skip CI triggers for sync/merge.")] + public bool? SkipCi { get; set; } + + public SyncBranches() + { + Dependency[] dependencies = JsonConvert.DeserializeObject(File.ReadAllText("dependencies.json")); + + _excludedPaths = Array.ConvertAll(dependencies, d => $"../{d.Path}"); + } + + public void Run() + { + SkipCi ??= false; + + Console.WriteLine($"Syncing {Target} branch to {Base} branch..."); + + Git.Fetch(Base); + + Git.Checkout(Base); + + Git.Fetch(Target); + + Git.Checkout(Target); + + foreach (string path in _excludedPaths) + { + Git.Checkout(Base, path); + + Git.Add(path); + } + + Git.Commit("Exclude Paths - Auto Commit", skipCi: SkipCi.Value, allowEmpty: true); + + string message = $"Sync to {Base} - Auto Commit"; + + if (SkipCi.Value) + { + message += " [skip ci]"; + } + + Git.Merge(Base, message); + + Git.Push(); + + Console.WriteLine($"Synced {Target} branch to {Base} branch completed."); + } +} \ No newline at end of file diff --git a/Setup/SyncDependencies.cs b/Setup/SyncDependencies.cs index fe154c8b3..a8abc8d55 100644 --- a/Setup/SyncDependencies.cs +++ b/Setup/SyncDependencies.cs @@ -5,6 +5,9 @@ namespace Setup; +/// +/// Generate and copy dependencies to Sample Project. +/// public class SyncDependencies : IRunnable { public int Order => 0; diff --git a/Setup/Utils/Utilities.cs b/Setup/Utils/Utilities.cs index aab767060..e2e59ff00 100644 --- a/Setup/Utils/Utilities.cs +++ b/Setup/Utils/Utilities.cs @@ -11,15 +11,7 @@ public static class Utilities { public static void Run(this string command) { - if (!Git.Enabled) - { - if (command.ToLower().StartsWith("git")) - { - Console.WriteLine($"Git disabled skipping command: {command}"); - - return; - } - } + Console.WriteLine($"Running Command : {command}"); // Tried switch statement couldn't find a way to make it work if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) @@ -37,8 +29,8 @@ public static void Run(this string command) throw new Exception($"[Unsupported OS] Can't run command: {command}"); } } - - public static void RunWithPowershell(this string command) + + private static void RunWithPowershell(this string command) { using (PowerShell powerShell = PowerShell.Create()) { @@ -62,7 +54,7 @@ public static void RunWithPowershell(this string command) /// /// Command to run. /// If command fails. - public static void RunWithBash( this string command) + private static void RunWithBash( this string command) { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { diff --git a/scripts/setup.bat b/scripts/setup.bat index f94a29e1c..a26225e22 100644 --- a/scripts/setup.bat +++ b/scripts/setup.bat @@ -14,6 +14,6 @@ if "%1"=="" ( set "config=%1" ) -dotnet run -sync_dependencies -git:disabled -c %config% Setup.csproj +dotnet run -s -g false -c %config% Setup.csproj popd diff --git a/scripts/setup.sh b/scripts/setup.sh index f1ef9469c..cf07865e2 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -7,6 +7,6 @@ git submodule update --init pushd "$scripts_dir"/../Setup # publish DLLs to unity package -dotnet run -sync_dependencies -git:disabled -c ${1:-Release} Setup.csproj +dotnet run -s -g false -c ${1:-Release} popd \ No newline at end of file