From 7371c44183c2ae29e5903ca367a5debe89b83713 Mon Sep 17 00:00:00 2001 From: Jeremy Chou Date: Fri, 30 Dec 2022 23:20:53 +0800 Subject: [PATCH] [ioctl] Build contract prepare command line into new ioctl (#3737) * [ioctl] build contract prepare command line into new ioctl * build unittest to cover the modification * fix commit * update assert message * update solc version * update solc message version * format * brew install solidity Co-authored-by: huofei <68298506@qq.com> --- install-solc.sh | 13 ++-- ioctl/newcmd/contract/contractprepare.go | 61 +++++++++++++++++++ ioctl/newcmd/contract/contractprepare_test.go | 33 ++++++++++ 3 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 ioctl/newcmd/contract/contractprepare.go create mode 100644 ioctl/newcmd/contract/contractprepare_test.go diff --git a/install-solc.sh b/install-solc.sh index e4bd339dff..01ce9ede51 100755 --- a/install-solc.sh +++ b/install-solc.sh @@ -13,8 +13,8 @@ set -e -LINUX_RELEASES_URL="https://github.com/ethereum/solidity/releases/download/v0.4.25/solidity-ubuntu-trusty.zip" -WINDOWS_RELEASES_URL="https://github.com/ethereum/solidity/releases/download/v0.4.25/solidity-windows.zip" +LINUX_RELEASES_URL="https://github.com/ethereum/solidity/releases/download/v0.8.17/solc-static-linux" +WINDOWS_RELEASES_URL="https://github.com/ethereum/solidity/releases/download/v0.8.17/solc-windows.exe" INSTALL_DIRECTORY='/usr/local/bin' downloadJSON() { @@ -106,7 +106,7 @@ if [ "$OS" = "darwin" ]; then brew update brew upgrade brew tap ethereum/ethereum - brew install solidity@5 + brew install solidity else if [ "${OS}" != "linux" ] && { [ "${ARCH}" = "ppc64" ] || [ "${ARCH}" = "ppc64le" ];}; then # ppc64 and ppc64le are only supported on Linux. @@ -125,17 +125,16 @@ else DOWNLOAD_FILE=$(mktemp) downloadFile "$BINARY_URL" "$DOWNLOAD_FILE" - unzip -o "$DOWNLOAD_FILE" -d /tmp echo "Setting executable permissions." - chmod +x /tmp/"$INSTALL_NAME" + chmod +x "$DOWNLOAD_FILE" if [ "$OS" = "windows" ]; then echo "Moving executable to $HOME/$INSTALL_NAME" - mv /tmp/"$INSTALL_NAME" "$HOME/$INSTALL_NAME" + mv "$DOWNLOAD_FILE" "$HOME/$INSTALL_NAME" else echo "Moving executable to $INSTALL_DIRECTORY/$INSTALL_NAME" - sudo mv /tmp/"$INSTALL_NAME" "$INSTALL_DIRECTORY/$INSTALL_NAME" + sudo mv "$DOWNLOAD_FILE" "$INSTALL_DIRECTORY/$INSTALL_NAME" fi fi diff --git a/ioctl/newcmd/contract/contractprepare.go b/ioctl/newcmd/contract/contractprepare.go new file mode 100644 index 0000000000..48bc7548a9 --- /dev/null +++ b/ioctl/newcmd/contract/contractprepare.go @@ -0,0 +1,61 @@ +// Copyright (c) 2022 IoTeX Foundation +// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability +// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. +// This source code is governed by Apache License 2.0 that can be found in the LICENSE file. + +package contract + +import ( + "os/exec" + + "github.com/pkg/errors" + "github.com/spf13/cobra" + + "github.com/iotexproject/iotex-core/ioctl" + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/util" +) + +// Multi-language support +var ( + _prepareCmdShorts = map[config.Language]string{ + config.English: "Prepare solidity compiler", + config.Chinese: "准备solidity编译器", + } +) + +// NewContractPrepareCmd represents the contract prepare command +func NewContractPrepareCmd(client ioctl.Client) *cobra.Command { + short, _ := client.SelectTranslation(_prepareCmdShorts) + + return &cobra.Command{ + Use: "prepare", + Short: short, + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + _, err := util.SolidityVersion(_solCompiler) + if err != nil { + cmdString := "curl --silent https://raw.githubusercontent.com/iotexproject/iotex-core/master/install-solc.sh | sh" + installCmd := exec.Command("bash", "-c", cmdString) + cmd.Println("Preparing solidity compiler ...") + + err = installCmd.Run() + if err != nil { + return errors.Wrap(err, "failed to prepare solc") + } + } + solc, err := util.SolidityVersion(_solCompiler) + if err != nil { + return errors.Wrap(err, "solidity compiler not ready") + } + if !checkCompilerVersion(solc) { + return errors.Errorf("unsupported solc version %d.%d.%d, expects solc version 0.8.17\n", + solc.Major, solc.Minor, solc.Patch) + } + + cmd.Println("Solidity compiler is ready now.") + return nil + }, + } +} diff --git a/ioctl/newcmd/contract/contractprepare_test.go b/ioctl/newcmd/contract/contractprepare_test.go new file mode 100644 index 0000000000..072ac0e294 --- /dev/null +++ b/ioctl/newcmd/contract/contractprepare_test.go @@ -0,0 +1,33 @@ +// Copyright (c) 2022 IoTeX Foundation +// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability +// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. +// This source code is governed by Apache License 2.0 that can be found in the LICENSE file. + +package contract + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" + + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/util" + "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" +) + +func TestNewContractPrepareCmd(t *testing.T) { + skipWithoutSolc(t) + require := require.New(t) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + client := mock_ioctlclient.NewMockClient(ctrl) + client.EXPECT().SelectTranslation(gomock.Any()).Return("contract", config.English) + + t.Run("prepare contract", func(t *testing.T) { + cmd := NewContractPrepareCmd(client) + result, err := util.ExecuteCmd(cmd) + require.NoError(err) + require.Equal("Solidity compiler is ready now.\n", result) + }) +}