diff --git a/.github/workflows/publish_releases.yml b/.github/workflows/publish_releases.yml new file mode 100644 index 0000000..bc706c1 --- /dev/null +++ b/.github/workflows/publish_releases.yml @@ -0,0 +1,91 @@ +name: Publish releases + +on: + push: + tags: + - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 + +jobs: + publish: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + - name: Set env + run: echo ::set-env name=RELEASE_VERSION::$(echo ${GITHUB_REF:11}) + - name: Set workspace env + run: echo ::set-env name=GITHUB_WORKSPACE::$(echo ${GITHUB_WORKSPACE}) + - name: Change NuGet package version + run: sed -i -E 's/()[^>]+(<\/Version>)/\1${{ env.RELEASE_VERSION }}\2/g' ${GITHUB_WORKSPACE}/badger.csproj && cat ${GITHUB_WORKSPACE}/badger.csproj + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '3.1.401' + # Runs a single command using the runners shell + - name: Publish osx-x64 + run: dotnet publish -c release -r osx-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true + - name: zip osx-x64 + run: zip --junk-paths badger-osx-x64.zip ${GITHUB_WORKSPACE}/bin/release/netcoreapp3.1/osx-x64/publish/badger + - name: Publish win-x64 + run: dotnet publish -c release -r win-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true + - name: zip win-x64 + run: zip --junk-paths badger-win-x64.zip ${GITHUB_WORKSPACE}/bin/release/netcoreapp3.1/win-x64/publish/badger.exe + - name: Publish linux-x64 + run: dotnet publish -c release -r linux-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true + - name: zip linux-x64 + run: zip --junk-paths badger-linux-x64.zip ${GITHUB_WORKSPACE}/bin/release/netcoreapp3.1/linux-x64/publish/badger + - name: Create release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + draft: false + prerelease: false + - name: Upload osx-x64 assets + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./badger-osx-x64.zip + asset_name: badger-osx-x64.zip + asset_content_type: application/zip + - name: Upload win-x64 assets + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./badger-win-x64.zip + asset_name: badger-win-x64.zip + asset_content_type: application/zip + - name: Upload linux-x64 assets + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./badger-linux-x64.zip + asset_name: badger-linux-x64.zip + asset_content_type: application/zip + - name: Install dependencies + run: dotnet restore + - name: Build + run: dotnet build -c release --no-restore + - name: Publish badger + uses: rohith/publish-nuget@v2 + with: + PROJECT_FILE_PATH: ${{env.GITHUB_WORKSPACE}}/badger.csproj + NUGET_KEY: ${{ secrets.NUGET_API_KEY }} + - name: Publish to GitHub + uses: tanaka-takayoshi/nuget-publish-to-github-packages-action@v2.1 + with: + nupkg-path: ${{env.GITHUB_WORKSPACE}}/bin/release/*.nupkg + repo-owner: 'hex' + gh-user: 'hex' + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb4a814 --- /dev/null +++ b/.gitignore @@ -0,0 +1,367 @@ +# Created by .ignore support plugin (hsz.mobi) +### VisualStudio template +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +.idea/ +/badger.csproj-E diff --git a/Assets/badger.png b/Assets/badger.png new file mode 100644 index 0000000..4ee2b92 Binary files /dev/null and b/Assets/badger.png differ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b17e2bd --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Alexandru Geana + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..c0d5a6e --- /dev/null +++ b/Program.cs @@ -0,0 +1,240 @@ +using System; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.IO; +using System.Linq; +using ImageMagick; + +namespace Badger +{ + internal class BadgeOptions + { + public string Text { get; set; } + public string Icon { get; set; } + public string Color { get; set; } + public string TextColor { get; set; } + private string _textAlignment; + + public string TextAlignment + { + get => _textAlignment; + set => + _textAlignment = value switch + { + "top" => "North", + "left" => "West", + "bottom" => "South", + "right" => "East", + "topLeft" => "Northwest", + "topRight" => "Northeast", + "bottomLeft" => "Southwest", + "bottomRight" => "Southeast", + "center" => "Center", + _ => _textAlignment + }; + } + + public int Angle { get; set; } + public int OffsetX { get; set; } + public int OffsetY { get; set; } + + private string _position; + + public string Position + { + get => _position; + set => + _position = value switch + { + "top" => "North", + "left" => "West", + "bottom" => "South", + "right" => "East", + "topLeft" => "Northwest", + "topRight" => "Northeast", + "bottomLeft" => "Southwest", + "bottomRight" => "Southeast", + "center" => "Center", + _ => _position + }; + } + + public bool Replace { get; set; } + } + + internal static class Program + { + private const string OutputDir = "badgerOutput"; + + private static void AddAllOptions(Command command) + { + command.AddOption(Color()); + command.AddOption(TextColor()); + command.AddOption(TextAlignment()); + command.AddOption(Angle()); + command.AddOption(OffsetX()); + command.AddOption(OffsetY()); + command.AddOption(Position()); + command.AddOption(Replace()); + + Option Color() => + new Option(new[] {"-c", "--color"}, () => "#4096EE", + "Set badge color with a hexadecimal color code"); + + Option TextColor() => new Option(new[] {"-t", "--text-color",}, + () => "#F9F7ED", + "Set badge text color with a hexadecimal color code"); + + Option TextAlignment() => new Option(new[] {"-l", "--text-alignment",}, + () => "center", + "Set badge text alignment"); + + Option Angle() => new Option(new[] {"-a", "--angle"}, () => 0, "Set badge rotation"); + Option OffsetX() => new Option(new[] {"-x", "--offset-x"}, () => 0, "Set badge x-axis offset"); + Option OffsetY() => new Option(new[] {"-y", "--offset-y"}, () => 0, "Set badge y-axis offset"); + Option Position() => new Option(new[] {"-p", "--position"}, () => "bottom", "Set badge position"); + Option Replace() => new Option(new[] {"-r", "--replace"}, () => false, "Replace input icon"); + } + + private static void EmptyDirectory(string path) + { + var di = new DirectoryInfo(path); + + foreach (var file in di.GetFiles()) + { + file.Delete(); + } + + foreach (var dir in di.GetDirectories()) + { + dir.Delete(true); + } + } + + private static void CreateBadge(BadgeOptions options) + { + Directory.CreateDirectory(OutputDir); + EmptyDirectory(OutputDir); + + var topSettings = new MagickReadSettings + { + BackgroundColor = new MagickColor(options.Color), + FontPointsize = 40, + FillColor = new MagickColor(options.Color), + Width = 1520 + }; + + var bottomSettings = new MagickReadSettings + { + BackgroundColor = new MagickColor(options.Color), + TextGravity = Enum.Parse(options.TextAlignment), + FontWeight = FontWeight.Bold, + FontPointsize = 180, + AntiAlias = true, + FillColor = new MagickColor(options.TextColor), + TextInterlineSpacing = 10, + Width = 1520 + }; + + using (var top = new MagickImage($"caption:-", topSettings)) + { + using (var bottom = new MagickImage($"caption:{options.Text}", bottomSettings)) + { + using (var badge = new MagickImageCollection()) + { + bottom.BorderColor = new MagickColor(options.Color); + bottom.Border(100, 0); + + badge.Add(top); + badge.Add(bottom); + + using (var result = badge.AppendVertically()) + { + result.BackgroundColor = MagickColors.Transparent; + // result.Rotate(options.Angle); + + result.Write($"{OutputDir}{Path.DirectorySeparatorChar}badge.png"); + } + } + } + } + } + + private static void AppendBadge(BadgeOptions options, string path) + { + using (var icon = new MagickImage(path)) + { + var badge = new MagickImage($"{OutputDir}{Path.DirectorySeparatorChar}badge.png"); + + badge.Resize(icon.Width + icon.Width / 2, icon.Height + icon.Height / 2); + badge.BackgroundColor = MagickColors.Transparent; + badge.Rotate(options.Angle); + + var offsetX = icon.Width * options.OffsetX / 100d; + var offsetY = icon.Height * options.OffsetY / 100d; + icon.Composite( + badge, + Enum.Parse(options.Position), + new PointD(offsetX, offsetY), + CompositeOperator.Over + ); + + Console.WriteLine( + $"Writing to: {(options.Replace ? path : $"{OutputDir}{Path.DirectorySeparatorChar}{Path.GetFileName(path)}")}"); + + icon.Write(options.Replace + ? path + : $"{OutputDir}{Path.DirectorySeparatorChar}{Path.GetFileName(path)}"); + } + } + + private static int Main(string[] args) + { + var rootCommand = new RootCommand + { + Description = "A command-line tool that adds labels to your app icon", + Name = "badger" + }; + + rootCommand.AddArgument(new Argument("text", "Set label text")); + rootCommand.AddArgument(new Argument("icon", + "Set path to icon with format .png | .jpg | .jpeg | .appiconset")); + + AddAllOptions(rootCommand); + + rootCommand.Handler = CommandHandler.Create( + (BadgeOptions badgeOptions) => + { + CreateBadge(badgeOptions); + + var fileAttributes = File.GetAttributes(badgeOptions.Icon); + + if (fileAttributes.HasFlag(FileAttributes.Directory)) + { + var files = Directory.EnumerateFiles(badgeOptions.Icon, "*.*", SearchOption.AllDirectories) + .Where(s => s.EndsWith(".png") || s.EndsWith(".jpg") || s.EndsWith(".jpeg")); + + foreach (var file in files) + { + AppendBadge(badgeOptions, file); + } + } + else + { + AppendBadge(badgeOptions, badgeOptions.Icon); + } + + if (badgeOptions.Replace) + { + Directory.Delete(OutputDir, true); + } + else + { + File.Delete($"{OutputDir}{Path.DirectorySeparatorChar}badge.png"); + } + }); + + return rootCommand.InvokeAsync(args).Result; + } + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f887431 --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +![GitHub](https://img.shields.io/github/license/hex/badger?style=flat-square) +![Language](https://img.shields.io/badge/language-C%23-blue?style=flat-square) +![Platform](https://img.shields.io/badge/platform-windows%20%7C%20macos%20%7C%20linux-lightgrey?style=flat-square) + +

+Badger +

+ + +# Badger +A cross-platform command-line tool that adds labels to your app icon. + +Badger is powered by [ImageMagick](http://www.imagemagick.org/) through [Magick.NET](https://github.com/dlemstra/Magick.NET) +## Installation +### macOS +#### Homebrew +```sh +brew tap hex/formulae +brew install badger +``` +### Windows +#### Scoop +```sh +scoop bucket add badger https://github.com/hex/Badger +scoop install badger +``` +## Options +`-c, --color` Set badge background color (supports hexadecimal color codes and [color names](https://imagemagick.org/script/color.php#color_names)) + +`-t, --text-color` Set badge label text color (supports hexadecimal color codes and [color names](https://imagemagick.org/script/color.php#color_names)) + +`-l, --text-alignment` Set badge label text alignment, supported values: `top, left, bottom, right, topLeft, topRight, bottomLeft, bottomRight, center` + +`-a, --angle` Set badge rotation + +`-x, --offset-x` Set x-axis offset + +`-y, --offset-y` Set y-axis offset + +`-p, --angle` Set badge position, supported values: `top, left, bottom, right, topLeft, topRight, bottomLeft, bottomRight, center` + +`-r, --replace` Set this to `true` if you want to overwrite the input file(s) +## Usage + +```sh +$ badger Alpha icon.png +$ badger Beta /assets/icons --angle 15 --color '#6BBA70' --text-color '#F9F7ED' --replace true + +Usage: + badger [options] + +Arguments: + Set label text + Set path to icon with format .png | .jpg | .jpeg | .appiconset + +Options: + -c, --color Set badge color with a hexadecimal color code [default: #4096EE] + -t, --text-color Set badge text color with a hexadecimal color code [default: #F9F7ED] + -l, --text-alignment Set badge text alignment [default: center] + -a, --angle Set badge rotation [default: 0] + -x, --offset-x Set badge x-axis offset [default: 0] + -y, --offset-y Set badge y-axis offset [default: 0] + -p, --position Set badge position [default: bottom] + -r, --replace Replace input icon [default: False] + --version Show version information + -?, -h, --help Show help and usage information +``` +## License + +Badger is released under the MIT license. See [LICENSE](https://github.com/hex/badger/blob/master/LICENSE) for more information. diff --git a/badger.csproj b/badger.csproj new file mode 100644 index 0000000..f6ad4b7 --- /dev/null +++ b/badger.csproj @@ -0,0 +1,32 @@ + + + + Exe + netcoreapp3.1 + disable + true + 2020.9.2 + badger + Alexandru Geana + A command-line tool that adds labels to your app icon + Alexandru Geana + https://github.com/hex/badger + https://github.com/hex/badger + https://github.com/hex/badger + git + badger + badger + badger + badger + 2020.9.3 + + + + + + + $(ProjectRuntimeConfigFilePath) + + + + diff --git a/badger.json b/badger.json new file mode 100644 index 0000000..cf38c83 --- /dev/null +++ b/badger.json @@ -0,0 +1,18 @@ +{ + "homepage": "https://github.com/hex/Badger", + "license": { + "identifier": "MIT", + "url": "https://opensource.org/licenses/MIT" + }, + "description": "Command-line tool that adds labels to your app icon", + "version": "2020.9.2", + "bin": [ + "badger.exe" + ], + "architecture": { + "64bit": { + "hash": "6a2612b13fb50e009d501e1928b34e393690b0f0172b323b628e6afa39b6ce15", + "url": "https://github.com/hex/badger/releases/download/v2020.9.2/badger-win-x64.zip" + } + } +} \ No newline at end of file