doudao1369 2017-11-23 14:46
浏览 78
已采纳

脚本无法更改项目结构

I have the following file make.sh which is working on the following project:

myapp
   utils
      run.go
      auth.go
   server.go
   make.sh

When I run this script it creates the expected tar and everything is working!

#!/bin/sh
go get ./...

rm -r /tmp/myapp
rm /tmp/myapp.tar.gz
mkdir /tmp/myapp
go build -o /tmp/myapp/myapp_mac
env GOOS=windows GOARH=amd64 go build -o /tmp/myapp/myapp_win64.exe
env GOOS=linux GOARCH=amd64 go build -o /tmp/myapp/myapp
cp -R ./resources /tmp/myapp/
cd /tmp
tar -czf myapp.tar.gz myapp

Now I needed to change the project structure to the following:

myapp
  make.sh
  src
    utils
       run.go
       auth.go
     server
       server.go

Now when I run the ‘make.sh’ it I got an error:

can't load package: package myapp: no Go files in /Users/i023333/go/src/myapp

Any idea how to adapt it?

I try to put the make.sh inside the server folder as is and it create the tar but its not valid…any idea what should I change the script here to adopt to the new project structure?

EDIT1

Before the structure which is generated is like following

tmp 
  myapp
    myapp
    myapp_mac
    myapp_win64.exe
  myapp.tar.gz

After trying the script in the answer of Charles Duffy I got the following

tmp 
  myapp
    myapp
    myapp_mac
    myapp_win64.exe

The tar file is missing, any idea ?

  • 写回答

3条回答 默认 最新

  • dongpian2559 2017-11-25 15:01
    关注

    To change your script, add the line:

    cd src/server || exit
    

    ...before any action which needs to care about the source. Thus:

    #!/bin/sh
    cd src/server || exit        # <--- ADD THIS LINE
    go get ./... || exit
    
    rm -rf /tmp/myapp      # aside: using hardcoded names in /tmp is a Really Bad Idea.
    rm -f /tmp/myapp.tar.gz
    mkdir -p /tmp/myapp
    go build -o /tmp/myapp/myapp_mac || exit
    GOOS=windows GOARH=amd64 go build -o /tmp/myapp/myapp_win64.exe || exit
    GOOS=linux GOARCH=amd64 go build -o /tmp/myapp/myapp || exit
    cp -R ./resources /tmp/myapp/ || exit
    cd /tmp || exit
    tar -czf /tmp/myapp.tar.gz myapp
    

    That said, I would suggest instead writing this as:

    #!/bin/bash
    basedir=$(cd -- "${BASH_SOURCE%/*}" && pwd) || exit
    
    rm -rf -- "$basedir/build" || exit
    mkdir -p -- "$basedir/"{build,dist} || exit
    
    build() (cd src/server && GOOS=$1 GOARCH=$2 exec go build -o "$basedir/build/$3")
    
    build darwin  amd64 myapp_mac   || exit
    build linux   amd64 myapp       || exit
    build windows amd64 myapp_win64 || exit
    
    if [[ -d "$basedir/resources" ]]; then
      cp -PR -- "$basedir/resources/." "$basedir/build/resources" || exit
    fi
    
    tar -czf "$basedir/dist/myapp.tar.gz" -C "$basedir/build" .
    

    Note:

    • We're operating relative to the script's location, so it works even if run from a different directory (you can run ./myapp/build, and it'll set builddir to the path to ./myapp).
    • We're not using /tmp (if we wanted to do so safely, then we'd have something like builddir=$(mktemp -t -d myapp-build.XXXXXX), and then rm -rf "$builddir" at the end).
    • We're using a #!/bin/bash shebang, which permits extensions such as [[ ]] and $BASH_SOURCE, instead of #!/bin/sh.
    • Repeated operations are encapsulated in a function.
    • We always die on errors we don't expect (which includes failing to copy resources if resources doesn't exist), but don't die on errors we do expect (such as filing to copy resources because it doesn't exist).
    • The exit status of the script is always the exit status of the last command, so there's no need for explicit error handling there.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题