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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应