From f61385332843af3100f990335e03c583e980d34e Mon Sep 17 00:00:00 2001 From: Michael Schwarz Date: Thu, 4 Dec 2014 00:40:56 +0100 Subject: Easier way to specify how files are generated. --- generate_sources.sh | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/generate_sources.sh b/generate_sources.sh index 687f91a..2d33554 100755 --- a/generate_sources.sh +++ b/generate_sources.sh @@ -1,9 +1,22 @@ #! /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 + "${generate_command[@]}" + fi +} + +# Call generate_file for each file to be generated. +# E.g.: +# generate_file src/test.scad echo "cube();" -- cgit From 260351e7db666c32d2a389e1d77884a6fe74d380 Mon Sep 17 00:00:00 2001 From: Michael Schwarz Date: Thu, 4 Dec 2014 00:41:09 +0100 Subject: Added documentation for generating files. --- readme.creole | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/readme.creole b/readme.creole index 55c4542..148e6d3 100644 --- a/readme.creole +++ b/readme.creole @@ -41,6 +41,13 @@ use 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. -- cgit From b3f93f02fd2362664e98611845ac09beb894552a Mon Sep 17 00:00:00 2001 From: Michael Schwarz Date: Thu, 4 Dec 2014 10:17:40 +0100 Subject: Let the script to generate the sources also open the resulting files. --- Makefile | 2 +- generate_sources.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 37a6547..81cbe15 100644 --- a/Makefile +++ b/Makefile @@ -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 2d33554..67fa258 100755 --- a/generate_sources.sh +++ b/generate_sources.sh @@ -13,7 +13,7 @@ function generate_file() { if ! [ "$current_file_name" ]; then echo "$file_name" elif [ "$current_file_name" == "$file_name" ]; then - "${generate_command[@]}" + "${generate_command[@]}" > "$file_name" fi } -- cgit From b15e1c4a98abe6ed13be76d583c8ce35067fbddd Mon Sep 17 00:00:00 2001 From: Michael Schwarz Date: Thu, 4 Dec 2014 10:18:08 +0100 Subject: Make all generated files, even if they're not used by the make file. --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 81cbe15..a06a0d1 100644 --- a/Makefile +++ b/Makefile @@ -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 -- cgit From 2c62798f699bc934d428458e3839df6f72095935 Mon Sep 17 00:00:00 2001 From: Michael Schwarz Date: Thu, 4 Dec 2014 10:18:30 +0100 Subject: Create necessary directories before generating files. --- generate_sources.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/generate_sources.sh b/generate_sources.sh index 67fa258..8ac6afa 100755 --- a/generate_sources.sh +++ b/generate_sources.sh @@ -13,6 +13,7 @@ function generate_file() { 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 } -- cgit From 9da7f66feec3ec222760e5d34ff83e43ab41af39 Mon Sep 17 00:00:00 2001 From: Michael Schwarz Date: Thu, 4 Dec 2014 10:18:44 +0100 Subject: Added an example for gnerating OpenSCAD files. --- .gitignore | 3 +++ generate_sources.sh | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a6a369a..90abfc2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ *.pyc *.dxf *.stl + +# Generated files +/src/generated/ diff --git a/generate_sources.sh b/generate_sources.sh index 8ac6afa..4a05760 100755 --- a/generate_sources.sh +++ b/generate_sources.sh @@ -19,5 +19,7 @@ function generate_file() { } # Call generate_file for each file to be generated. -# E.g.: -# generate_file src/test.scad echo "cube();" +for i in {1..5}; do + size=$[ $i * 10 ] + generate_file "src/generated/cube_$size.scad" echo "cube($size);" +done -- cgit