Skip to content

Commit

Permalink
Focus on being a CLI and support for use from an SPM plugin instead o…
Browse files Browse the repository at this point in the history
…f Tuist plugin (#2)

* Run SwiftFormat and SwiftLint via Mise

* Remove Tuist plugin code

* Update swiftformat config

* Remove handling for tuist exectuable from main function

* Remove Tuist references from README

* Update contributing guide

* CI lint workflow using Mise

* Add Mint installation instructions

* Script to build zipped binary and artifact bundle

* Update release workflow to include zipped binary and SPM artifact bundle

* Lint CI step should run lint command, rather than format
  • Loading branch information
lordcodes authored Mar 3, 2024
1 parent 48cc07c commit 528a228
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 95 deletions.
29 changes: 18 additions & 11 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,35 @@ jobs:
with:
ref: ${{ needs.prepare-release.outputs.commit_hash }}

- name: Install Tuist
- name: Build and package release
id: build
if: ${{ github.event.inputs.publish == 'true' }}
run: |
bash <(curl -Ls https://install.tuist.io)
- name: Tuist archive
if: ${{ github.event.inputs.publish == 'true' }}
run: |
tuist plugin archive
./Scripts/package-release.sh
- name: Create GitHub Release
if: ${{ github.event.inputs.publish == 'true' }}
uses: softprops/action-gh-release@v1
with:
token: "${{ secrets.GITHUB_TOKEN }}"
files: cloak.tuist-plugin.zip
files: |
"~/${{ steps.build.outputs.FILENAME }}"
"~/${{ steps.build.outputs.ARTIFACT_BUNDLE_FILENAME }}"
name: ${{ needs.prepare-release.outputs.version }}
tag_name: v${{ needs.prepare-release.outputs.version }}
body: ${{ needs.prepare-release.outputs.changelog }}
target_commitish: ${{ needs.prepare-release.outputs.commit_hash }}

- uses: actions/upload-artifact@v3
- name: Upload binary
uses: actions/upload-artifact@v3
with:
name: ${{ steps.build.outputs.FILENAME }}
path: "~/${{ steps.build.outputs.FILENAME }}"
retention-days: 5

- name: Upload SPM artifact bundle
uses: actions/upload-artifact@v3
with:
name: cloak.tuist-plugin.zip
path: cloak.tuist-plugin.zip
name: ${{ steps.build.outputs.ARTIFACT_BUNDLE_FILENAME }}
path: "~/${{ steps.build.outputs.ARTIFACT_BUNDLE_FILENAME }}"
retention-days: 5
21 changes: 5 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,12 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Install Tuist
run: |
bash <(curl -Ls https://install.tuist.io)
- name: Tuist fetch
run: |
tuist fetch
- name: SwiftFormat
run: |
mkdir -p .build/reports
tuist swiftformat lint --report .build/reports/swiftformat.json
- uses: jdx/mise-action@v2
with:
experimental: true

- name: SwiftLint
run: |
mkdir -p .build/reports
tuist swiftlint --reporter html > .build/reports/swiftlint.html
- name: Lint
run: mise r lint

- uses: actions/upload-artifact@v3
with:
Expand Down
9 changes: 9 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tools]
swiftformat = "0.53.1"
swiftlint = "0.54.0"

[settings]
legacy_version_file = false
yes = true
disable_tools = ['node', 'ruby', 'go']
experimental = true
9 changes: 9 additions & 0 deletions .mise/tasks/format
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
# mise description="Run code formatting"
# mise sources=["**/*.swift"]

OUTPUT_DIR=".build/lint/reports"
mkdir -p "${OUTPUT_DIR}"

swiftlint --fix --strict --quiet --reporter html --output "${OUTPUT_DIR}/swiftlint.html"
swiftformat . --report "${OUTPUT_DIR}/swiftformat.json"
9 changes: 9 additions & 0 deletions .mise/tasks/lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
# mise description="Run code linting"
# mise sources=["**/*.swift"]

OUTPUT_DIR=".build/lint/reports"
mkdir -p "${OUTPUT_DIR}"

swiftformat . --lint --report "${OUTPUT_DIR}/swiftformat.json"
swiftlint --strict --quiet --reporter html --output "${OUTPUT_DIR}/swiftlint.html"
2 changes: 1 addition & 1 deletion .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

# file options

--exclude .build,Derived,Tuist/Dependencies
--exclude .build
1 change: 0 additions & 1 deletion .tuist-version

This file was deleted.

12 changes: 7 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ If this is your first time contributing to Cloak Swift, please have a read throu

## Reporting a bug

* Compare the version you have installed with `tuist cloak version` and check it against [the latest version](https://github.com/lordcodes/cloak-swift/releases).
* Compare the version you have installed with `cloakswift version` and check it against [the latest version](https://github.com/lordcodes/cloak-swift/releases).
* Check the issue hasn't [already been reported](https://github.com/lordcodes/cloak-swift/issues).
* Check there isn't already an [open pull request](https://github.com/lordcodes/cloak-swift/pulls) to fix the issue.
* [Open an issue](https://github.com/lordcodes/cloak-swift/issues/new/choose) providing as much information as possible.
Expand All @@ -21,9 +21,11 @@ If this is your first time contributing to Cloak Swift, please have a read throu

* Ensure you have the latest version of Swift installed, the easiest way is via Xcode.
* Clone the repository.
* To build: build in Xcode or run `tuist plugin build`.
* To test: test in Xcode or run `tuist plugin test`.
* To format code: `tuist swiftformat`.
* To run locally, navigate to the project you want to run on and then run `tuist plugin run tuist-cloak {COMMAND} --path /path/to/cloak-swift`.
* Ensure you have required CLI tools: `mise install`.
* To build: build in Xcode or run `swift build`.
* To test: test in Xcode or run `swift test`.
* To format code: `mise r format`.
* To lint code: `mise r lint`.
* To run locally, navigate to the project you want to run on and then run `/path/to/cloakswift {COMMAND}`.

Thanks!
4 changes: 0 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ let package = Package(
name: "cloak",
platforms: [.macOS(.v11)],
products: [
.executable(
name: "tuist-cloak",
targets: ["CloakCLI"]
),
.executable(
name: "cloakswift",
targets: ["CloakCLI"]
Expand Down
5 changes: 0 additions & 5 deletions Plugin.swift

This file was deleted.

56 changes: 17 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

---

This is **Cloak Swift** - a tool and Tuist plugin to encrypt secrets and then pass them in an obfuscated form into Swift applications.
This is **Cloak Swift** - a CLI tool to encrypt secrets and then pass them in an obfuscated form into Swift applications.

&nbsp;

Expand Down Expand Up @@ -47,24 +47,6 @@ The generated Swift uses obfuscation of the values rather than as raw strings.

## Install

The primary intention was to use Cloak Swift as a [Tuist](https://github.com/tuist/tuist) plugin, however, it can also be used as a standard CLI tool as well.

### ▶︎ 🖥 As a Tuist Plugin

To set up as a Tuist plugin in your project simply follow the [Tuist plugin install instructions](https://docs.tuist.io/plugins/using-plugins/) using the [latest version](https://github.com/lordcodes/cloak-swift/releases/latest).

Add the plugin to `Config.swift`.

```swift
import ProjectDescription

let config = Config(
plugins: [
.git(url: "https://github.com/lordcodes/cloak-swift.git", tag: "vTAG")
]
)
```

### ▶︎ 🖥 Standalone via Swift Package Manager

Cloak Swift can be easily installed globally using Swift Package Manager.
Expand All @@ -79,6 +61,14 @@ This will install cloakswift into `/usr/local/bin`. If you get a permission erro

You can uninstall it again using `make uninstall` which simply deletes it from `/usr/local/bin`.

### ▶︎ 🍀 Mint

You can install Cloak Swift on MacOS using [Mint](https://github.com/yonaskolb/Mint) as follows:

```terminal
mint install lordcodes/cloak-swift
```

### ▶︎ 🍺 Homebrew

Support for Homebrew may be planned in the future.
Expand Down Expand Up @@ -133,16 +123,14 @@ You should also add your encryption key to this file using the key name `CLOAK_E

If the secret keys are specified in the required keys file `secret-keys`, then they will be read as environment variables as well, where the environment variables take precendence. This is useful in a CI environment where you can specify them as environment variables and avoid having to write them to a file as you would locally.

IMPORTANT NOTE: The secrets aren't read as environment variables correctly when using Cloak as a Tuist plugin, due to the environment Tuist plugins are executed in. Therefore, it is best to write the secrets to a file in a setup step of your CI workflow.

The best practice is that the values should be encrypted first.

### 🖥 Via the Tuist Plugin
### 🖥 Via the Standalone CLI

Run Cloak's tasks via Tuist. The tool will check paths relative to the working directory for the `.cloak` directory configured above.
Run Cloak's tasks via a CLI. The tool will check paths relative to the working directory for the `.cloak` directory configured above.

```terminal
USAGE: tuist cloak <subcommand> [-q|--quiet]
USAGE: cloakswift <subcommand> [-q|--quiet]
SUBCOMMANDS:
createkey Create encryption key.
Expand All @@ -155,41 +143,31 @@ OPTIONS:
-q, --quiet Silence any output except errors
```

You can obtain help using `tuist cloak --help` and also obtain help for each subcommand using `tuist cloak <subcommand> --help`.
You can obtain help using `cloakswift --help` and also obtain help for each subcommand using `cloakswift <subcommand> --help`.

#### Create encryption key

Generates an encryption key, that can then be used within your project to encrypt secrets. This key is then passed into your app so that you can decrypt them at runtime.

`tuist cloak createkey`
`cloakswift createkey`

#### Encrypt a value

Provide a value and the encrypted version will be returned. Your encryption key should be provided as described above.

`tuist cloak encrypt <value>`
`cloakswift encrypt <value>`

#### Decrypt an encrypted value

Provide an encrypted value and the decrypted version will be returned. Your encryption key should be provided as described above.

`tuist cloak decrypt <encrypted>`
`cloakswift decrypt <encrypted>`

#### Generate a secrets file in-app

Generate a Swift file that can be used to access your secrets within your app at runtime. Certain aspects of the generated file can be customised using the `config` file as described above. The secrets will be obfuscated and included as `[UInt8]`, but with Swift properties to return them as `String` in their usable form.

`tuist cloak generate`

### 🖥 Via the Standalone CLI

Run Cloak's tasks via a standalone executable. The tool will check paths relative to the working directory for the `.cloak` directory configured above.

```terminal
USAGE: cloakswift <subcommand> [-q|--quiet]
```

Same usage as the Tuist plugin, except `tuist cloak` is replaced with `cloakswift`.
`cloakswift generate`

### 📦 As a Swift Package

Expand Down
12 changes: 12 additions & 0 deletions Scripts/get-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

set -e

REGEX="static let current = \"([0-9]+.[0-9]+.[0-9]+)\""
while IFS= read -r line; do
if [[ $line =~ $REGEX ]]
then
echo "${BASH_REMATCH[1]}"
break
fi
done < Sources/CloakKit/Version/Version.swift
51 changes: 51 additions & 0 deletions Scripts/package-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh

set -e

VERSION=$(./Scripts/get-version.sh)
ARTIFACT_BUNDLE=cloakswift-$VERSION.artifactbundle
ARTIFACT_BUNDLE_BIN=$ARTIFACT_BUNDLE/cloakswift/bin
BINARY_DIR=bin
OUTPUT_BINARY=$BINARY_DIR/cloakswift

mkdir -p $BINARY_DIR
mkdir -p $ARTIFACT_BUNDLE
mkdir -p $ARTIFACT_BUNDLE_BIN

# Copy files into bundle
cp LICENSE $ARTIFACT_BUNDLE
cp README.md $ARTIFACT_BUNDLE
cp CHANGELOG.md $ARTIFACT_BUNDLE

# Create bundle info.json from template, replacing version
INFO_TEMPLATE=Scripts/spm-artifact-bundle-info.template
sed 's/__VERSION__/'"${VERSION}"'/g' $INFO_TEMPLATE > "${ARTIFACT_BUNDLE}/info.json"

# Build multi-arch binary
swift build --disable-sandbox -c release --arch arm64
swift build --disable-sandbox -c release --arch x86_64
lipo -create -output $OUTPUT_BINARY .build/arm64-apple-macosx/release/cloakswift .build/x86_64-apple-macosx/release/cloakswift
strip -rSTX ${OUTPUT_BINARY}

# Copy macOS binary into bundle
cp $OUTPUT_BINARY $ARTIFACT_BUNDLE_BIN

# ZIP binary
FILENAME="cloakswift-v$VERSION.zip"
zip -r -j $FILENAME $OUTPUT_BINARY

# ZIP artifact bundle
ARTIFACT_BUNDLE_FILENAME="cloakswift-v$VERSION.artifactbundle.zip"
zip -r -X $ARTIFACT_BUNDLE_FILENAME $ARTIFACT_BUNDLE

# Clean-up
rm -rf $BINARY_DIR
rm -rf $ARTIFACT_BUNDLE

if [ -z "${GITHUB_ACTIONS}" ]; then
echo "Binary created: $FILENAME"
echo "SPM artifact bundle created: $ARTIFACT_BUNDLE_FILENAME"
else
echo "FILENAME=${FILENAME}" >> $GITHUB_OUTPUT
echo "ARTIFACT_BUNDLE_FILENAME=${ARTIFACT_BUNDLE_FILENAME}" >> $GITHUB_OUTPUT
fi
15 changes: 15 additions & 0 deletions Scripts/spm-artifact-bundle-info.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"schemaVersion": "1.0",
"artifacts": {
"cloakswift": {
"version": "__VERSION__",
"type": "executable",
"variants": [
{
"path": "cloakswift/bin/cloakswift",
"supportedTriples": ["x86_64-apple-macosx", "arm64-apple-macosx"]
}
]
}
}
}
6 changes: 1 addition & 5 deletions Sources/CloakCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,5 @@ private func extractProgramName() -> String {
guard let program = CommandLine.arguments.first else {
exit(EXIT_SUCCESS)
}
var programName = URL(fileURLWithPath: program).lastPathComponent
if programName == "tuist-cloak" {
programName = "tuist cloak"
}
return programName
return URL(fileURLWithPath: program).lastPathComponent
}
8 changes: 0 additions & 8 deletions Tuist/Config.swift

This file was deleted.

0 comments on commit 528a228

Please sign in to comment.