Skip to content

Instantly share code, notes, and snippets.

@pote
Last active October 13, 2015 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pote/79265bc8e09a9571a60e to your computer and use it in GitHub Desktop.
Save pote/79265bc8e09a9571a60e to your computer and use it in GitHub Desktop.
Golang binary embeddable views
#!/usr/bin/env bash
echo "package views
const $(echo "$1" | tr a-z A-Z) = \`$(cat views/$1.html)\`" > src/views/$1.go
##
# This Makefile is a slimmed down version from an actual project, to make deployment
# easier all the html files in `views/` are copied into Go files as constants and
# placed under `src/views/`, assuming `GOPATH="$PWD/.deps:$PWD" (1) this means that in
# your code you can get the contents of `views/index.html` as `views.INDEX`.
#
# There is also how I handle dependencies with gpm, which is unrelated, but I though
# I'd leave it for the hell of it.
#
# (1) You don't need to have that exact GOPATH, but "$PWD" does need to be included in
# it so you can import "views" from `src/views` and use it.
VIEWS = $(patsubst views/%.html,src/views/%.go,$(wildcard views/*.html))
.deps/.installed: Godeps
gpm install && touch $@
build: *.go .deps/.installed $(VIEWS)
go build -o <your_binary_name>
##
# Views -> Go files
##
src/views:
mkdir -p $@
src/views/%.go: views/%.html
#echo 'package views\n\nconst $(shell echo $* | tr a-z A-Z) = `$(shell cat views/$*.html)`' > $@
./asset_to_go "$*"
@inkel
Copy link

inkel commented Oct 13, 2015

Tené en echo a secas no interpreta los \n:

inkel@miralejos ~/tmp
$ echo "package views\n\nconst $(git ls-files lib)"
package views\n\nconst lib/disyuntor.rb
lib/disyuntor/version.rb
lib/rack/disyuntor.rb

Para eso necesitás usar -e, así:

inkel@miralejos ~/tmp
$ echo -e "package views\n\nconst $(git ls-files lib)"
package views

const lib/disyuntor.rb
lib/disyuntor/version.rb
lib/rack/disyuntor.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment