Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ioctl] Build contract prepare command line into new ioctl #3737

Merged
merged 11 commits into from
Dec 30, 2022
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)
})
}