Skip to content

Commit

Permalink
CLI Packaging + CI/CD (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
smals-mavh authored Nov 20, 2024
1 parent f36f51a commit b381c45
Show file tree
Hide file tree
Showing 20 changed files with 341 additions and 152 deletions.
37 changes: 0 additions & 37 deletions .github/workflows/openapi-maven-release.yml

This file was deleted.

95 changes: 95 additions & 0 deletions .github/workflows/rest-guide-validator-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Release belgif openapi projects to Maven Central when a version tag is created
name: rest-guide-validator-release
on:
push:
tags:
- 'v*'
jobs:
build-and-deploy-jars:
runs-on: ubuntu-latest
steps:
# Setup Maven action to specify maven version > required for BUG https://issues.apache.org/jira/browse/MNG-7055
- name: Setup Maven Action
uses: s4u/[email protected]
with:
java-version: '17'
java-distribution: 'adopt'
maven-version: '3.9.9'
cache-enabled: 'false'
settings-servers: |
[{
"id": "ossrh",
"username": "${{ secrets.OPENAPI_OSSRH_USERNAME }}",
"password": "${{ secrets.OPENAPI_OSSRH_TOKEN }}"
}]
- name: Build and deploy with Maven
run: mvn -B deploy -Drevision=${GITHUB_REF_NAME:1} -Prelease
env:
MAVEN_GPG_KEY: ${{ secrets.OPENAPI_OSSRH_GPG_SECRET_KEY }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.OPENAPI_OSSRH_GPG_SECRET_KEY_PASSWORD }}
MAVEN_CENTRAL_USER: ${{ secrets.OPENAPI_OSSRH_USERNAME }}
MAVEN_CENTRAL_TOKEN: ${{ secrets.OPENAPI_OSSRH_TOKEN }}
- id: upload-cli-folder
name: Upload CLI Folder
uses: actions/upload-artifact@v4
with:
path: |
./cli/package/
./cli/target/belgif-rest-guide-validator-cli-*.jar
./cli/target/libs/
!./cli/target/belgif-rest-guide-validator-cli-*-*.jar
name: cli-folder
retention-days: 1
overwrite: 'true'
if-no-files-found: 'error'

build-windows-installer:
runs-on: windows-latest
needs: [build-and-deploy-jars]
steps:
- name: Setup JDK
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'adopt'
- name: Download CLI Folder
uses: actions/download-artifact@v4
with:
name: cli-folder
- name: Display structure of downloaded files
run: ls -R
- name: Set environment variables
run: |
$env:SEMANTIC_VERSION=$env:GITHUB_REF_NAME.Substring(1)
echo "RELEASED_VERSION=$env:SEMANTIC_VERSION" >> $env:GITHUB_ENV
- name: Package Windows Installer
run: |
jlink --add-modules java.base,java.naming,java.xml,java.desktop --output ./target/custom-jre --strip-debug --no-header-files --no-man-pages --verbose
jpackage --input ./target --name belgif-validator-rest --main-jar belgif-rest-guide-validator-cli-$env:RELEASED_VERSION.jar --type msi --app-version $env:RELEASED_VERSION --description "Validate OpenApi to Belgif guidelines" --vendor "Belgif" --icon ./package/belgif.ico --win-console --resource-dir "./package/windows" --runtime-image ./target/custom-jre --dest ./target --install-dir belgif-rest-guide-validator --file-associations package\file-associations\FAyaml.properties --file-associations package\file-associations\FAjson.properties --add-launcher belgif-validate-openapi="./package/cli-launcher.properties" --win-per-user-install --verbose
ren target\belgif-validator-rest-$env:RELEASED_VERSION.msi belgif-rest-guide-validator-$env:RELEASED_VERSION.msi
- id: upload-windows-installer
name: Upload Windows Installer
uses: actions/upload-artifact@v4
with:
path: ./target/belgif-rest-guide-validator-${{ env.RELEASED_VERSION }}.msi
name: windows-installer
retention-days: 1
overwrite: 'true'
if-no-files-found: 'error'


create-release:
runs-on: ubuntu-latest
needs: [build-windows-installer]
steps:
- uses: actions/checkout@v4
- name: Download Windows Installer
uses: actions/download-artifact@v4
with:
name: windows-installer
- name: Create GitHub release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release create ${GITHUB_REF} --draft --title "Release ${GITHUB_REF_NAME}" --notes "Changes:
- [placeholder]" "belgif-rest-guide-validator-${GITHUB_REF_NAME:1}.msi#belgif-rest-guide-validator-${GITHUB_REF_NAME:1}.msi"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ tmp/
*.jar
*.war

*.log
*.log
*.msi
19 changes: 19 additions & 0 deletions BUILDING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
This project is build using Maven and GitHub actions.
The release process of this project is documented [here](https://github.com/belgif/openapi-common/blob/master/BUILDING.md).

To build the windows installer locally, invoke the following command in the cli/target folder after a maven build.

To inspect which modules are actually needed for the jar to run:
```bash
jdeps --print-module-deps --ignore-missing-deps .\belgif-rest-guide-validator-cli-latest.jar
```

To build a custom JRE with only the needed dependencies use
```bash
jlink --add-modules java.base,java.naming,java.xml,java.desktop --output custom-jre --strip-debug --no-header-files --no-man-pages
```

To build the installer with a custom JRE (for windows) use:
```bash
jpackage --input . --name belgif-validator-rest --main-jar belgif-rest-guide-validator-cli-latest.jar --type msi --app-version 2.2.0 --description "Validate OpenApi to Belgif guidelines" --vendor "Belgif" --icon ../package/belgif.ico --win-console --resource-dir "../package/windows" --runtime-image custom-jre --install-dir belgif-rest-guide-validator --file-associations ..\package\file-associations\FAyaml.properties --file-associations ..\package\file-associations\FAjson.properties --add-launcher belgif-validate-openapi="../package/cli-launcher.properties" --win-per-user-install
```
4 changes: 0 additions & 4 deletions cli/buildinfo.md

This file was deleted.

1 change: 1 addition & 0 deletions cli/package/cli-launcher.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main-class=io.github.belgif.rest.guide.validator.cli.BelgifRestGuideCli
3 changes: 3 additions & 0 deletions cli/package/file-associations/FAjson.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mime-type=application/json
extension=json
description=JSON document
3 changes: 3 additions & 0 deletions cli/package/file-associations/FAyaml.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mime-type=application/yaml
extension=yaml
description=YAML document
15 changes: 15 additions & 0 deletions cli/package/windows/main.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
InstallScope="$(var.JpInstallScope)" Platform="x64"
/>

<!-- Start modification to start application post-install -->

<CustomAction Id="LaunchApplication"
ExeCommand='&quot;[INSTALLDIR]belgif-validate-openapi.exe&quot; --postInstall'
Directory="INSTALLDIR"
Impersonate="yes"
Return="asyncNoWait"/>

<!-- End modification to start application post-install -->

<Media Id="1" Cabinet="Data.cab" EmbedCab="yes" />

<Upgrade Id="$(var.JpProductUpgradeCode)">
Expand Down Expand Up @@ -126,6 +136,11 @@
<Custom Action="JpDisallowDowngrade" After="FindRelatedProducts">JP_DOWNGRADABLE_FOUND</Custom>
<?endif?>
<RemoveExistingProducts Before="CostInitialize"/>
<!-- Start modification to start postInstall -->

<Custom Action="LaunchApplication" After="InstallFinalize">Not Installed</Custom>

<!-- End modification to start postInstall -->
</InstallExecuteSequence>

</Product>
Expand Down
3 changes: 2 additions & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<outputDirectory>
${project.build.directory}/libs
</outputDirectory>
<includeScope>compile</includeScope>
</configuration>
</execution>
</executions>
Expand All @@ -91,7 +92,7 @@
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs</classpathPrefix>
<mainClass>io.github.belgif.rest.guide.validator.cli.BelgifRestGuideCli</mainClass>
<mainClass>io.github.belgif.rest.guide.validator.cli.BelgifRestGuideValidator</mainClass>
</manifest>
</archive>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Callable;


Expand All @@ -27,22 +28,41 @@ public class BelgifRestGuideCli implements Callable<Integer> {
@CommandLine.Mixin
private ValidatorOptions options;

/**
* postInstall flag is used by the windows installer to start the application after installation.
* This will show a welcome message.
*/
@CommandLine.Option(names = {"--postInstall"}, hidden = true)
private boolean postInstall;

@Override
public Integer call() {
try {
if (executeRules()) {
return 0;
} else {
return 11;
}
} catch (Exception e) {
log.error(e.getMessage());
if (postInstall) {
printPostInstall();
return 0;
}
if (options.getFiles() == null || options.getFiles().isEmpty()) {
log.error("belgif-rest-guide-validator requires at least one file. Use command \"belgif-validate-openapi --help\" for more information.");
return 1;
}
else {
int returnCode;
log.info("Using: belgif-rest-guide-validator-{}", VersionProvider.getValidatorVersion());
try {
if (executeRules()) {
returnCode = 0;
} else {
returnCode = 11;
}
} catch (Exception e) {
log.error(e.getMessage());
returnCode = 1;
}
return returnCode;
}
}

public static void main(String[] args) {
printCommandLineArguments(args);
int exitCode = new CommandLine(new BelgifRestGuideCli()).execute(args);
System.exit(exitCode);
}
Expand All @@ -61,15 +81,6 @@ private boolean executeRules() throws FileNotFoundException {
return runner.executeRules();
}

private static void printCommandLineArguments(String[] args) {
log.info("Using: belgif-rest-guide-validator-{}", VersionProvider.getValidatorVersion());
StringBuilder sb = new StringBuilder();
for (String arg : args) {
sb.append(arg).append("\t");
}
log.info("Arguments: {}", sb);
}

/**
* Makes sure that the enum can be selected case-insensitive in the CLI.
*/
Expand All @@ -81,4 +92,15 @@ private List<OutputType> initOutputTypes() {
return types;
}

private void printPostInstall() {
log.info("belgif-rest-guide-validator-{} successfully installed!", VersionProvider.getValidatorVersion());
log.info("\nUse command: 'belgif-validate-openapi' followed by a file name or path to start.");
log.info("Use 'belgif-validate-openapi --help' for all options.");
log.info("\nRight click on an openapi or swagger file and open with 'Validate OpenApi to Belgif guidelines'");
log.info("In some cases you'll have to set this up manually by selecting 'Choose another app' -> 'Choose an app on your PC' -> navigate to belgif-rest-guide-validator folder in C:\\Users\\user-name\\AppData\\Local\\ -> 'belgif-validator-rest.exe'");
log.info("\n\n========================");
log.info("\nPress Enter to exit...");
new Scanner(System.in).nextLine();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.belgif.rest.guide.validator.cli;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;

import java.util.Scanner;

public class BelgifRestGuideValidator {

private static final Logger log = LoggerFactory.getLogger(BelgifRestGuideValidator.class);

public static void main(String[] args) {
int exitCode = new CommandLine(new BelgifRestGuideCli()).execute(args);
log.info("\nPress Enter to exit...");
new Scanner(System.in).nextLine();
System.exit(exitCode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@Getter
public class ValidatorOptions {

@CommandLine.Parameters(paramLabel = "file", defaultValue = "openapi.yaml", description = "File(s) or folder(s), space separated. For a folder all json and yaml files will be used.")
@CommandLine.Parameters(paramLabel = "file", description = "File(s) or folder(s), space separated. For a folder all json and yaml files will be used.")
private List<File> files;

@CommandLine.Option(names = {"-e", "--excludedFile"}, description = "File(s) or folder(s) to exclude from validation. For multiple values, repeat -e or --excludedFile. Use of wildcards is possible.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

class BelgifRestGuideCliTest {

Expand All @@ -34,6 +33,7 @@ void testHelp() {
int exitCode = cmd.execute("--help");
assertEquals(0, exitCode);
assertTrue(sw.toString().contains("Options:"));
assertFalse(sw.toString().contains("postInstall"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ValidatorOptionsTest {
@Test
void testDefaultValues() {
ValidatorOptions options = CommandLine.populateCommand(new ValidatorOptions());
assertEquals("openapi.yaml", options.getFiles().get(0).getName());
assertNotNull(options.getExcludedFiles());
assertTrue(options.getExcludedFiles().isEmpty());
assertEquals("console", options.getOutputTypes().get(0));
Expand Down
4 changes: 2 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser</artifactId>
<version>2.1.22</version>
<version>2.1.23</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>2.2.21</version>
<version>2.2.25</version>
</dependency>
<dependency>
<groupId>org.openapitools.empoa</groupId>
Expand Down
Loading

0 comments on commit b381c45

Please sign in to comment.