GeneratedFile.rb
1 # Copyright (C) 2018 Apple Inc. All rights reserved. 2 # 3 # Redistribution and use in source and binary forms, with or without 4 # modification, are permitted provided that the following conditions 5 # are met: 6 # 1. Redistributions of source code must retain the above copyright 7 # notice, this list of conditions and the following disclaimer. 8 # 2. Redistributions in binary form must reproduce the above copyright 9 # notice, this list of conditions and the following disclaimer in the 10 # documentation and/or other materials provided with the distribution. 11 # 12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 13 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 14 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 16 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 19 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 20 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 21 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 22 # THE POSSIBILITY OF SUCH DAMAGE. 23 24 require 'date' 25 require 'digest' 26 27 $LICENSE = <<-EOF 28 Copyright (C) #{Date.today.year} Apple Inc. All rights reserved. 29 30 Redistribution and use in source and binary forms, with or without 31 modification, are permitted provided that the following conditions 32 are met: 33 34 1. Redistributions of source code must retain the above copyright 35 notice, this list of conditions and the following disclaimer. 36 2. Redistributions in binary form must reproduce the above copyright 37 notice, this list of conditions and the following disclaimer in the 38 documentation and/or other materials provided with the distribution. 39 40 THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 41 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 42 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 44 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 45 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 47 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 49 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 EOF 51 52 module GeneratedFile 53 class Template < Struct.new(:multiline_comment, :line_comment, :prefix, :suffix, :body) 54 def initialize 55 super(["/*", " *", "*/"], "// ", nil, nil, nil) 56 end 57 end 58 59 def self.create(filename, *dependencies) 60 template = Template.new 61 yield template 62 63 file = File.open(filename, "w") 64 self.license(file, template, dependencies) 65 66 unless template.prefix.nil? 67 write(file, template.prefix.to_s, "\n") 68 end 69 unless template.body.nil? 70 write(file, template.body.to_s, "\n") 71 end 72 unless template.suffix.nil? 73 write(file, template.suffix.to_s, "\n") 74 end 75 76 file.fsync 77 self.sha1(file, template, dependencies) 78 end 79 80 def self.sha1(file, template, dependencies) 81 write(file, template.line_comment, " SHA1Hash: ", Digest::SHA1.hexdigest(dependencies.join), "\n") 82 end 83 84 def self.license(file, template, dependencies) 85 unless template.multiline_comment.nil? 86 write(file, template.multiline_comment[0], "\n") 87 end 88 89 comment = if template.multiline_comment.nil? then template.line_comment else template.multiline_comment[1] end 90 write(file, $LICENSE.strip.split("\n").map { |line| "#{comment} #{line}" }.join("\n"), "\n\n") 91 write(file, comment, " Autogenerated, do not modify.\n") 92 93 unless template.multiline_comment.nil? 94 write(file, template.multiline_comment[2], "\n") 95 end 96 97 write(file, "\n") 98 end 99 100 def self.write(file, *strings) 101 file.write(strings.map(&:to_s).join) 102 end 103 end 104