From 70313bfc399e1831abafe276d3420f268de251c2 Mon Sep 17 00:00:00 2001
From: kakulisen <18813972746@163.com>
Date: Thu, 12 Dec 2019 22:39:36 +0800
Subject: [PATCH 1/3] [SCB-1663] add scripts
Signed-off-by: kakulisen <18813972746@163.com>
---
README-ZH.md | 17 +++---
README.md | 17 +++---
cli/scripts/cli.cmd | 67 +++++++++++++++++++++++
cli/scripts/cli.sh | 37 +++++++++++++
toolkit-distribution/src/assembly/bin.xml | 8 +++
5 files changed, 130 insertions(+), 16 deletions(-)
create mode 100644 cli/scripts/cli.cmd
create mode 100755 cli/scripts/cli.sh
diff --git a/README-ZH.md b/README-ZH.md
index 8fc4c2cd..1dcd7704 100644
--- a/README-ZH.md
+++ b/README-ZH.md
@@ -234,13 +234,14 @@ mvn toolkit:verify
### 3.3 使用toolkit cli工具
-可执行jar包位于toolkit/cli/target/bin目录下
+* 如果你使用的是正式发布版本(>=0.2.0),可以在解压二进制包后直接使用cli.sh
+* 如果你是通过源码构建,可以将`cli/scripts/cli.sh`与`cli/target/bin/toolkit-cli-{version}.jar`放置在同一目录,然后使用cli.sh
```shell
-$ java -jar toolkit-cli-{version}.jar help
+$ ./cli.sh help
```
#### 3.3.1 契约生成微服务工程
```shell
-$ java -jar toolkit-cli-{version}.jar codegenerate -m ServiceComb -i swagger.yaml -o ./project -p SpringMVC
+$ ./cli.sh codegenerate -m ServiceComb -i swagger.yaml -o ./project -p SpringMVC
```
> **codegenerate** 命令选项说明:
* -m, --microservice-framework : 指定微服务框架,现支持ServiceComb
@@ -266,7 +267,7 @@ $ java -jar toolkit-cli-{version}.jar codegenerate -m ServiceComb -i swagger.ya
#### 3.3.2 契约生成文档
```shell
-$ java -jar toolkit-cli-{version}.jar docgenerate -i swagger.yaml -o ./document
+$ ./cli.sh docgenerate -i swagger.yaml -o ./document
```
> **docgenerate** 命令选项说明:
* -i, --input : 指定遵循OpenAPI规范的契约文件,支持yaml和json格式,支持指定本地和网络文件
@@ -279,9 +280,9 @@ $ java -jar toolkit-cli-{version}.jar docgenerate -i swagger.yaml -o ./document
#### 3.3.3 契约风格检查
```shell
-$ java -jar toolkit-cli-{version}.jar checkstyle -r style-check-rules.yaml -f oas.yaml
+$ ./cli.sh checkstyle -r style-check-rules.yaml -f oas.yaml
or
-$ java -jar toolkit-cli-{version}.jar cs -r style-check-rules.yaml -f oas.yaml
+$ ./cli.sh cs -r style-check-rules.yaml -f oas.yaml
```
> **checkstyle** Command argument
@@ -293,9 +294,9 @@ $ java -jar toolkit-cli-{version}.jar cs -r style-check-rules.yaml -f oas.yaml
#### 3.3.4 契约兼容性检查
```shell
-$ java -jar toolkit-cli-{version}.jar checkcompatibility left-oas.yaml right-oas.yaml
+$ ./cli.sh checkcompatibility left-oas.yaml right-oas.yaml
或者
-$ java -jar toolkit-cli-{version}.jar cc left-oas.yaml right-oas.yaml
+$ ./cli.sh cc left-oas.yaml right-oas.yaml
```
> **checkcompatibility** Command argument
diff --git a/README.md b/README.md
index 6ee5217f..55123d70 100644
--- a/README.md
+++ b/README.md
@@ -229,13 +229,14 @@ mvn toolkit:verify
### 3.3 Use the toolkit cli
-The executable jar package is located in the toolkit/cli/target/bin directory
+* If you are using the official release version ( >=0.2.0 ), you can use cli.sh directly after decompressing the binary package
+* If you are building from source, you can put `cli/scripts/cli.sh` in the same directory as`cli/target/bin/toolkit-cli-{version}.jar` and then use cli.sh
```shell
-$ java -jar toolkit-cli-{version}.jar help
+$ ./cli.sh help
```
#### 3.3.1 Service contract generation microservice project
```shell
-$ java -jar toolkit-cli-{version}.jar codegenerate -m ServiceComb -i swagger.yaml -o ./project -p SpringMVC
+$ ./cli.sh codegenerate -m ServiceComb -i swagger.yaml -o ./project -p SpringMVC
```
> **codegenerate** Command option
* -m, --microservice-framework. Specify microservices framework, now supports ServiceComb.
@@ -261,7 +262,7 @@ e.g.:--service-type provider
#### 3.3.2 Service contract generation document
```shell
-$ java -jar toolkit-cli-{version}.jar docgenerate -i swagger.yaml -o ./document
+$ ./cli.sh docgenerate -i swagger.yaml -o ./document
```
> **docgenerate** Command option
* -i, --input. Specifies contract files that follow the OpenAPI specification, supports yaml and json formats, and supports specifying local and network files.
@@ -274,9 +275,9 @@ e.g:-f swagger-ui
#### 3.3.3 Service contract style checking
```shell
-$ java -jar toolkit-cli-{version}.jar checkstyle -r style-check-rules.yaml -f oas.yaml
+$ ./cli.sh checkstyle -r style-check-rules.yaml -f oas.yaml
or
-$ java -jar toolkit-cli-{version}.jar cs -r style-check-rules.yaml -f oas.yaml
+$ ./cli.sh cs -r style-check-rules.yaml -f oas.yaml
```
> **checkstyle** Command argument
@@ -288,9 +289,9 @@ See [style check rules](oas-validator/README.md)
#### 3.3.4 Service contract compatibility checking
```shell
-$ java -jar toolkit-cli-{version}.jar checkcompatibility left-oas.yaml right-oas.yaml
+$ ./cli.sh checkcompatibility left-oas.yaml right-oas.yaml
or
-$ java -jar toolkit-cli-{version}.jar cc left-oas.yaml right-oas.yaml
+$ ./cli.sh cc left-oas.yaml right-oas.yaml
```
> **checkcompatibility** Command argument
diff --git a/cli/scripts/cli.cmd b/cli/scripts/cli.cmd
new file mode 100644
index 00000000..0e07d359
--- /dev/null
+++ b/cli/scripts/cli.cmd
@@ -0,0 +1,67 @@
+@REM Licensed to the Apache Software Foundation (ASF) under one or more
+@REM contributor license agreements. See the NOTICE file distributed with
+@REM this work for additional information regarding copyright ownership.
+@REM The ASF licenses this file to You under the Apache License, Version 2.0
+@REM (the "License"); you may not use this file except in compliance with
+@REM the License. You may obtain a copy of the License at
+@REM
+@REM http://www.apache.org/licenses/LICENSE-2.0
+@REM
+@REM Unless required by applicable law or agreed to in writing, software
+@REM distributed under the License is distributed on an "AS IS" BASIS,
+@REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+@REM See the License for the specific language governing permissions and
+@REM limitations under the License.
+
+@echo off && rem
+
+set SUCCESS_CODE=0
+set ERROR_CODE=1
+
+@REM check if java install
+if "%JAVA_HOME%" == "" goto JAVA_HOME_ERROR
+if not exist "%JAVA_HOME%\bin\java.exe" goto JAVA_HOME_ERROR
+
+set jarFile=
+for /f "delims=" %%t in ('dir /S /B cli-*.jar') do set jarFile=%%t
+
+@REM check if jar file exist
+if "%jarFile%" == "" (
+ echo Error: cli.jar not exist. make sure it is placed in the current directory or subdirectory of this script
+ exit /B %ERROR_CODE%
+)
+
+set allparam=
+:param
+set str=%1
+if "%str%"=="" (
+ goto end
+)
+set allparam=%allparam% %str%
+shift /0
+goto param
+
+:end
+if "%allparam%"=="" (
+ goto eof
+)
+
+rem remove left right blank
+:intercept_left
+if "%allparam:~0,1%"==" " set "allparam=%allparam:~1%"&goto intercept_left
+
+:intercept_right
+if "%allparam:~-1%"==" " set "allparam=%allparam:~0,-1%"&goto intercept_right
+
+:eof
+
+java -jar %jarFile% %allparam%
+exit /B %SUCCESS_CODE%
+
+:JAVA_HOME_ERROR
+echo.
+echo Error: JAVA_HOME invalid in your environment. >&2
+echo Please set the JAVA_HOME variable in your environment to match the >&2
+echo location of your Java installation. >&2
+echo.
+exit /B %ERROR_CODE%
diff --git a/cli/scripts/cli.sh b/cli/scripts/cli.sh
new file mode 100755
index 00000000..9360bba1
--- /dev/null
+++ b/cli/scripts/cli.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements. See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License. You may obtain a copy of the License at
+##
+## http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+
+# check if java install
+if [ -z "$JAVA_HOME" -o ! -f "$JAVA_HOME/bin/java" ]; then
+ echo Error: JAVA_HOME invalid in your environment. >&2
+ echo Please set the JAVA_HOME variable in your environment to match the >&2
+ echo location of your Java installation. >&2
+ exit 1
+fi
+
+shellDir=$(dirname $(readlink -f "$0"))
+
+# check if jar file exist
+jarFile=$(find ${shellDir} -name "cli-*.jar" | head -n 1)
+
+if [[ ! -f ${jarFile} ]]; then
+ echo "error: cli.jar not exist. make sure it is placed in the current directory or subdirectory of this script"
+ exit 1
+fi
+
+java -jar ${jarFile} $*
diff --git a/toolkit-distribution/src/assembly/bin.xml b/toolkit-distribution/src/assembly/bin.xml
index 2c1ff530..25565b95 100644
--- a/toolkit-distribution/src/assembly/bin.xml
+++ b/toolkit-distribution/src/assembly/bin.xml
@@ -42,6 +42,14 @@
licenses/**
+
+ ../cli/scripts
+ /
+
+ cli.sh
+ cli.cmd
+
+
From ba99fb507d8b013c76aed81bae26d805e4f59323 Mon Sep 17 00:00:00 2001
From: kakulisen <18813972746@163.com>
Date: Fri, 13 Dec 2019 11:58:03 +0800
Subject: [PATCH 2/3] fix bug of script in Mac
Signed-off-by: kakulisen <18813972746@163.com>
---
cli/scripts/cli.sh | 15 ++++++++++++++-
.../servicecomb/toolkit/cli/ToolkitMain.java | 2 +-
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/cli/scripts/cli.sh b/cli/scripts/cli.sh
index 9360bba1..81752d16 100755
--- a/cli/scripts/cli.sh
+++ b/cli/scripts/cli.sh
@@ -24,7 +24,20 @@ if [ -z "$JAVA_HOME" -o ! -f "$JAVA_HOME/bin/java" ]; then
exit 1
fi
-shellDir=$(dirname $(readlink -f "$0"))
+# resolve links - $0 may be a softlink
+PRG="$0"
+
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`/"$link"
+ fi
+done
+
+shellDir=`dirname "$PRG"`
# check if jar file exist
jarFile=$(find ${shellDir} -name "cli-*.jar" | head -n 1)
diff --git a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
index 8b637e71..ac3ac01a 100755
--- a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
@@ -33,7 +33,7 @@ public static void main(String[] args) {
initialProjectVersion();
- Cli.CliBuilder builder = Cli.builder("java -jar toolkit-cli-" + projectVersion + ".jar");
+ Cli.CliBuilder builder = Cli.builder("cli.sh");
builder.withDescription("Microservice development toolkit(version " + projectVersion
+ "). ");
builder.withDefaultCommand(Help.class);
From 8d8a35d69ac6b9d43e64adb8f0646c4602331f7b Mon Sep 17 00:00:00 2001
From: kakulisen <18813972746@163.com>
Date: Fri, 13 Dec 2019 15:35:19 +0800
Subject: [PATCH 3/3] [SCB-1663] determine script name based on system property
Signed-off-by: kakulisen <18813972746@163.com>
---
README-ZH.md | 11 +++++++++--
README.md | 8 ++++++--
cli/scripts/cli.cmd | 2 +-
cli/scripts/cli.sh | 2 +-
.../apache/servicecomb/toolkit/cli/ToolkitMain.java | 11 ++++++++++-
5 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/README-ZH.md b/README-ZH.md
index 1dcd7704..f61eee00 100644
--- a/README-ZH.md
+++ b/README-ZH.md
@@ -234,8 +234,15 @@ mvn toolkit:verify
### 3.3 使用toolkit cli工具
-* 如果你使用的是正式发布版本(>=0.2.0),可以在解压二进制包后直接使用cli.sh
-* 如果你是通过源码构建,可以将`cli/scripts/cli.sh`与`cli/target/bin/toolkit-cli-{version}.jar`放置在同一目录,然后使用cli.sh
+
+#### 3.3.0 获取
+* 如果你使用的是正式发布版本(>=0.2.0),可以在解压二进制包后直接使用里面的脚本文件
+ * 在Linux及Mac环境,请使用cli.sh
+ * 在Windows环境,请使用cli.cmd
+
+* 如果你是通过源码构建,可以将`cli/scripts/cli.*`与`cli/target/bin/toolkit-cli-{version}.jar`放置在同一目录,然后根据不同环境选择不同脚本
+
+以下所有示例均通过Linux环境的cli.sh进行介绍
```shell
$ ./cli.sh help
```
diff --git a/README.md b/README.md
index 55123d70..015129a1 100644
--- a/README.md
+++ b/README.md
@@ -229,8 +229,12 @@ mvn toolkit:verify
### 3.3 Use the toolkit cli
-* If you are using the official release version ( >=0.2.0 ), you can use cli.sh directly after decompressing the binary package
-* If you are building from source, you can put `cli/scripts/cli.sh` in the same directory as`cli/target/bin/toolkit-cli-{version}.jar` and then use cli.sh
+* If you are using the official release version ( >=0.2.0 ), you can use the script files directly after decompressing the binary package
+ * In Linux and Mac environment, please use cli.sh
+ * In the Windows environment, please use cli.cmd
+* If you are building from source, you can put `cli/scripts/cli.*` in the same directory as`cli/target/bin/toolkit-cli-{version}.jar` and then choose different scripts according to different environments
+
+All the examples below are introduced through cli.sh for Linux environment
```shell
$ ./cli.sh help
```
diff --git a/cli/scripts/cli.cmd b/cli/scripts/cli.cmd
index 0e07d359..5d7e25ce 100644
--- a/cli/scripts/cli.cmd
+++ b/cli/scripts/cli.cmd
@@ -55,7 +55,7 @@ if "%allparam:~-1%"==" " set "allparam=%allparam:~0,-1%"&goto intercept_right
:eof
-java -jar %jarFile% %allparam%
+java -Dscript.name="cli.cmd" -jar %jarFile% %allparam%
exit /B %SUCCESS_CODE%
:JAVA_HOME_ERROR
diff --git a/cli/scripts/cli.sh b/cli/scripts/cli.sh
index 81752d16..cdc22c86 100755
--- a/cli/scripts/cli.sh
+++ b/cli/scripts/cli.sh
@@ -47,4 +47,4 @@ if [[ ! -f ${jarFile} ]]; then
exit 1
fi
-java -jar ${jarFile} $*
+java -Dscript.name="cli.sh" -jar ${jarFile} $*
diff --git a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
index ac3ac01a..851289d6 100755
--- a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
@@ -21,6 +21,8 @@
import java.util.Optional;
import java.util.Properties;
+import org.apache.commons.lang3.StringUtils;
+
import io.airlift.airline.Cli;
import io.airlift.airline.Help;
@@ -33,7 +35,14 @@ public static void main(String[] args) {
initialProjectVersion();
- Cli.CliBuilder builder = Cli.builder("cli.sh");
+ String scriptName = System.getProperty("script.name");
+ Cli.CliBuilder builder = null;
+ if (StringUtils.isNotEmpty(scriptName)) {
+ builder = Cli.builder(scriptName);
+ } else {
+ builder = Cli.builder("java -jar toolkit-cli-" + projectVersion + ".jar");
+ }
+
builder.withDescription("Microservice development toolkit(version " + projectVersion
+ "). ");
builder.withDefaultCommand(Help.class);