Skip to content

Commit

Permalink
[ioctl] Build contract prepare command line into new ioctl (#3737)
Browse files Browse the repository at this point in the history
* [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>
  • Loading branch information
LuckyPigeon and huof6829 committed Dec 30, 2022
1 parent 35c8528 commit 7371c44
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
13 changes: 6 additions & 7 deletions install-solc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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.
Expand All @@ -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

61 changes: 61 additions & 0 deletions ioctl/newcmd/contract/contractprepare.go
Original file line number Diff line number Diff line change
@@ -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
},
}
}
33 changes: 33 additions & 0 deletions ioctl/newcmd/contract/contractprepare_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 7371c44

Please sign in to comment.