         stream.write @headerValue + '\n\n' if @headerValue?
         stream.write "ninja_required_version = #{@version}\n" if @version?
         stream.write "builddir=#{@buildDir}\n" if @buildDir?
-        for clause in @clauses
+        for clause in [].concat(@rules, @edges, @variables)
             clause.write stream
         stream.write "default #{@defaultRule}\n" if @defaultRule?
 

    # build directory (where Ninja put logs and where you can put
    # intermediary products).
    constructor: (@version, @buildDir) ->
        @clauses = []
        @edgeCount = 0
        @ruleCount = 0


    # Add a variable assignation into `name` from the `value`.
    assign: (name, value) ->
        clause = new NinjaAssignBuilder(name, value)
        @variables.push clause
        clause

    # Add a rule and return it.
    rule: (name) ->
        clause = new NinjaRuleBuilder(name)
        @rules.push clause
        @ruleCount++
        clause

    # Add an edge and return it.
    edge: (targets) ->
        clause = new NinjaEdgeBuilder(targets)
        @edges.push clause
        @edgeCount++
        clause

