cespare / reflex

Run a command when files change
MIT License
3.38k stars 136 forks source link

Using sed to change filepath #70

Closed nathanmalishev closed 4 years ago

nathanmalishev commented 4 years ago

Hey there, This isn't an issue but i was hoping to get some advice. I currently have a golang repository consisting of lots of lambda functions. Currently when i make a change to one lambda function, i have to rebuild them all.

build: clean
        @for dir in `ls handler`; do \
            echo 'building...' $$dir; \
            CGO_ENABLED=0 GOOS=linux go build -o dist/handler/$$dir gitlab.com/repo/code/handler/$$dir; \
        done
        echo "All built"

gomon:
    reflex -R '^node_modules/' -R '^output/' -r '\.go' -s -- sh -c 'make build'

File structure being

-/
   -handler/
       -apiHandler1/
       -apiHandler2/

I think I am extremely close, in rebuilding just one file when it's changed. But am having trouble getting sed to work. When i try my variations with sed, i often get sed: 1: "s|[^/]+|g": unterminated substitute in regular expression

{} has the output of handler/approvedGet/approvedGet.go, but i need handler/approvedGet/ without the file name. For the command CGO_ENABLED=0 GOOS=linux go build -o dist/handler/approvedGet/approvedGet.go gitlab.com/repo/code/handler/approvedGet/

A sed like sed "s|[^/]+|g" would work but i'm having trouble getting it to execute.

I'm hoping to get to the point where i have both {} and another variable in the command without the filename, so i can easily rebuild just the files & packages that have been changed. Something like would be ideal

gomon:
    reflex -R '^node_modules/' -R '^output/' -r '\.go' -s -- 
        sh -c 'CGO_ENABLED=0 GOOS=linux go build -o dist/handler/{} gitlab.com/repo/code/{}_withoutfilename'

Thanks

cespare commented 4 years ago

Hey @nathanmalishev, happy to help. I'm going to close the issue since it's not a problem with reflex.

CGO_ENABLED=0 GOOS=linux go build -o dist/handler/handler/approvedGet/approvedGet.go gitlab.com/code/olp_api/handler/approvedGet/

This command confuses me. Why do you want to build a binary that's named approvedGet.go?

For doing what you want, would it be possible to use the dirname command? This might be simpler than using sed for this. Here's a demo:

reflex -r '\.txt$' -- bash -c 'echo subs={} basename=$(basename {}) dirname=$(dirname {})'

If I run

mkdir x/y
touch x/y/z.txt

then reflex prints

[00] subs=x/y/z.txt basename=z.txt dirname=x/y