Skip to content

Instantly share code, notes, and snippets.

@cjheath
Created December 6, 2018 04:19
Show Gist options
  • Save cjheath/71ef1d252a1f35a1715db2918fbb8dd3 to your computer and use it in GitHub Desktop.
Save cjheath/71ef1d252a1f35a1715db2918fbb8dd3 to your computer and use it in GitHub Desktop.
lib/tasks/cql.rake for a Rails Application
namespace 'cql' do
APPLICATION = 'my-app-name'
desc 'Check that there are no errors in CQL'
task :check do
system <<-END
set -x
cd "db/app"
schema_compositor --binary #{APPLICATION}.cql
END
end
desc 'Rebuild the diff after generating or editing a new schema.rb'
task :rediff do
system <<-END
set -x
cd "db"
diff -b -U1 schema.rb.generated schema.rb |
sed 's/20..-..-.. ..:..:...000000000 +1000/2000-00-00 00:00:00.000000000 +1000/' >schema.rb.diff
cd ..
diff -b -U0 app/models/.relations.generated app/models/relations |
sed 's/20..-..-.. ..:..:...000000000 +1000/2000-00-00 00:00:00.000000000 +1000/' >db/models.diff
END
end
desc "Generate and patch a new Rails schema.rb from the CQL source files"
task :schema do
# Create a new schema from CQL using active_facts
system <<-END or raise "Failed to regenerate schema from CQL"
set -x
cd "db/app"
mv -f ../schema.rb ../schema.rb.backup 2>/dev/null
schema_compositor --relational=surrogates --rails/schema=include_comments #{APPLICATION}.cql |
sed 's/version: 201.........../version: 20000000000000/' >../schema.rb &&
cd .. &&
cp schema.rb schema.rb.generated &&
( [ \! -s schema.rb.diff ] || patch < schema.rb.diff )
END
end
desc 'Regenerate CQL in single files from the CQL and ORM inputs'
task :omnibus do
system <<-END
set -x
cd "db/app"
mkdir omnibus >/dev/null 2>&1
afgen --cql #{APPLICATION}.cql > omnibus/#{APPLICATION}.cql.cql
afgen --cql ../norma/#{APPLICATION}.orm > omnibus/#{APPLICATION}.orm.cql
END
end
desc "Generate new Rails app/models/relations/*.rb from the CQL source files"
task :models do
system <<-END or raise "Failed to regenerate models from CQL"
mkdir -p app/models/relations
set -x
cd "db/app"
schema_compositor --relational=surrogates --rails/models=concern=Relations,output=../../app/models/relations #{APPLICATION}.cql &&
cd ../.. &&
rm -rf app/models/.relations.generated &&
cp -r app/models/relations/ app/models/.relations.generated &&
( [ \! -s db/models.diff ] || patch -p0 < db/models.diff )
END
end
desc "Generate the HTML glossary from CQL source files"
task :glossary do
system <<-END or raise "Failed to regenerate glossary from CQL"
set -x
mkdir -p public/documentation >/dev/null 2>&1
cd "db/app"
schema_compositor --relational --doc/glossary #{APPLICATION}.cql > ../../public/documentation/glossary.html
END
end
desc "Generate the highlighted CQL from the source"
task :highlighted do
system <<-END or raise "Failed to regenerate highlighted CQL"
set -x
mkdir -p public/documentation >/dev/null 2>&1
cd "db/app"
screen=`gem contents activefacts-generators |grep orm2.css`
#print=`gem contents activefacts |grep /print.css`
(
echo "<style media='screen' type='text/css'>"
cat "$screen"
echo "</style>"
#echo "<style media='print' type='text/css'>"
#cat "$print"
#echo "</style>"
echo "<pre>"
(echo /highlight; echo /load #{APPLICATION}.cql ) |
cql |
sed '
1,/Will show the highlighted parse/d
/span.*;$/{
N
s/;./;/
}
/^Loaded /d
'
echo "</pre>"
) > ../../public/documentation/#{APPLICATION}.cql.html
END
end
task :all => [ :schema, :models, :glossary ]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment