diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile | 8 | ||||
-rwxr-xr-x | generate_sources.sh | 30 | ||||
-rw-r--r-- | readme.creole | 7 |
4 files changed, 37 insertions, 11 deletions
@@ -2,3 +2,6 @@ *.pyc *.dxf *.stl + +# Generated files +/src/generated/ @@ -21,12 +21,12 @@ COMPILED_SCAD_FILES := $(filter-out $(LIBRARY_SCAD_FILES),$(SCAD_FILES)) STL_FILES := $(patsubst %.scad,%.stl,$(COMPILED_SCAD_FILES)) DXF_FILES := $(patsubst %.svg,%.dxf,$(SVG_FILES)) -# Everything. -all: $(STL_FILES) +# Everything. Also generates files which aren't compiled to anything else. +all: $(STL_FILES) $(GENERATED_FILES) # Everything^-1. clean: - rm -rf $(DXF_FILES) $(STL_FILES) $(GENERATED_SCAD_FILES) + rm -rf $(DXF_FILES) $(STL_FILES) $(GENERATED_FILES) # Needs to be included after target all has been defined. -include config.mk @@ -44,4 +44,4 @@ $(foreach i,$(COMPILED_SCAD_FILES),$(eval $(i): $(filter $(dir $(i))%,$(LIBRARY_ # Rule for automaticlaly generated OpenSCAD files. $(GENERATED_FILES): generate_sources.sh - ./generate_sources.sh $@ > $@ + ./generate_sources.sh $@ diff --git a/generate_sources.sh b/generate_sources.sh index 687f91a..4a05760 100755 --- a/generate_sources.sh +++ b/generate_sources.sh @@ -1,9 +1,25 @@ #! /usr/bin/env bash -if [ "$1" ]; then - # Print the content of the generated source named $1 here. - true -else - # Print a list of names of the files that should be generated using this script here. - true -fi +set -e -o pipefail + +current_file_name=$1 + +# This function should be called for each generated file with the file's name as the first argument and the command to call to produce the file's content as the remaining arguments. +function generate_file() { + file_name=$1 + shift + generate_command=("$@") + + if ! [ "$current_file_name" ]; then + echo "$file_name" + elif [ "$current_file_name" == "$file_name" ]; then + mkdir -p "$(dirname "$file_name")" + "${generate_command[@]}" > "$file_name" + fi +} + +# Call generate_file for each file to be generated. +for i in {1..5}; do + size=$[ $i * 10 ] + generate_file "src/generated/cube_$size.scad" echo "cube($size);" +done diff --git a/readme.creole b/readme.creole index 55c4542..148e6d3 100644 --- a/readme.creole +++ b/readme.creole @@ -41,6 +41,13 @@ use <filename> Please see the [http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Print_version|manual] for details. +== Generating Source files + +This template allows files to be generated automatically. Currently supported for inclusion in the build proess are OpenSCAD and SVG files. This works by editing the `generate_sources.sh` script, which is run by the Makefile was changed. + +The script should call the function `generate_file()` once for each file which should be generated. The first argument to the function should be the name of the file to be generated, the remaining arguments a command, which when run should output the file's content to standard output. How this function is called is up to the scrtip and may e.g. be done from a `for` loop or while iterating over a set of other source files. + + == Compiling To compile the whole project, run `make` from the directory in which this readme is. This will process all necessary SVG files and produce an STL file for each OpenSCAD source file. Individual files may be created or updated by passing their names to the make command, as ussual. |