Skip to content

Instantly share code, notes, and snippets.

@monkstone
Created July 9, 2017 13:52
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 monkstone/097d5d97489abd45cf375b7b7917d49e to your computer and use it in GitHub Desktop.
Save monkstone/097d5d97489abd45cf375b7b7917d49e to your computer and use it in GitHub Desktop.
require 'propane'
require 'fileutils'
require 'digest/sha1'
require 'active_support/all'
require 'erb'
require 'fileutils'
class Generator < Propane::App
def self.get_class_name
self.name.to_s
end
def get_class_name
self.class.get_class_name
end
def shape_count
ENV["SHAPE_COUNT"].nil? ? nil : ENV['SHAPE_COUNT'].to_i
end
def run_code
puts(@code) if ENV['DEBUG'].present?
eval @code
end
attr_accessor :path,:finished,:error
def version_count
ENV["VERSION_COUNT"].nil? ? 80_000 : ENV['VERSION_COUNT'].to_i
end
def rrange (min, max)
rand(min..max)
end
def r_setup # random setup of code
#TODO cut this up into smaller functions, this code is too fragile
code = ""
code << "background(#{rc},#{rc},#{rc})\n" if yn?
#code << "no_stroke\n" if yn? && code =~ /background/
#code << "no_fill\n" if yn? && code !~ /no_stroke/ #no_stroke and no_fill is a blank screen
#code << "fill(#{rc},#{rc},#{rc})\n" if yn? && code !~ /no_fill/ #no_stroke and no_fill is a blank screen
code
end
def rand_int min=1,max=255
end
def rh # random height
rand(0..height)
end
def rw # random width
rand(0..width)
end
def rhh # random height
rand(0..height)
"h(#{rand.round(2)})"
end
def rww # random width
"w(#{rand.round(2)})"
end
def rwht # random triangle
"triangle(#{rww},#{rhh},#{rww},#{rhh},#{rww},#{rhh})\n"
end
def rwhl # random line
"line(#{rww},#{rhh},#{rww},#{rhh})\n"
end
def rwhb # random bezier
"bezier(#{rww},#{rhh},#{rww},#{rhh},#{rww},#{rhh},#{rww},#{rhh})\n"
end
def rwhe r=nil # random triangle
r = rww if r.nil?
"ellipse(#{rww},#{rhh},#{r},#{r})\n"
end
def rwhr r=nil # random triangle
r = rww if r.nil?
"rect(#{rww},#{rhh},#{r},#{r})\n"
end
def rwhs # random shape
eval(%w[rwhe rwht rwhr].sample)
end
def rc # random color 0-255
rand(0..256)
end
def cs #clear screen; set it to white
fill 255,255,255
stroke(0)
rect(-1,-1,width+1,height+1)
#these set defaults back
fill(255)
end
def w v=1
width * v
end
def h v=1
height * v
end
def yn? prob=0.5 #.5 probably of true
rand > prob
end
#def initialize codefile=nil
# @codefile = codefile
#end
def pre_hook
#hook for us to add anything we want
end
def settings
pre_hook
ww = ENV['WIDTH'].present? ? ENV['WIDTH'] : 256
hh = ENV['HEIGHT'].present? ? ENV['HEIGHT'] : 256
puts "using width #{ww} and height #{hh}"
puts "count is set to #{version_count}"
puts "output dir is #{output_dir}"
puts "shape count is #{shape_count}"
size(ww.to_i,hh.to_i)
end
def inside_docker?
File.exists?("/.dockerenv")
end
def output_dir
ENV["OUTPUT_DIR"].present? ? ENV['OUTPUT_DIR'] : "#{ENV['HOME']}/data"
end
def save_code_and_image path=nil
path = output_dir
if @code
t = Digest::SHA1.hexdigest @code
char = t[0]
if inside_docker?
path = "/home/data/#{char}/#{t}/"
else
path = "#{path}/#{char}/#{t}/"
end
#puts path
FileUtils.mkdir_p path
FileUtils.touch("#{path}/#{get_class_name}") #TODO only run this in debug mode
File.open("#{path}/code.txt", 'w') { |file| file.write(@code) }
fp = "#{path}/image.jpg"
puts(fp) if ENV['DEBUG'].present?
save fp
result = RbConfig::CONFIG['host_os'] =~ /linux/ ? `identify #{fp}` : `jpeginfo -c #{fp}`
if File.zero?("#{path}/code.txt") || result.match(/ERROR/i)
puts "bad file, removing #{path}"
`rm -rf #{path}`
end
#puts fp
end
end
def setup
cs
file = ARGV[0]
@code = File.read(file)
run_code
save_code_and_image
end
end
if __FILE__ == $0
Generator.new
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment