Class: Zayin::Rake::Vagrant::PhpTasks

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/zayin/rake/vagrant/php.rb

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ PhpTasks

Returns a new instance of PhpTasks.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
# File 'lib/zayin/rake/vagrant/php.rb', line 10

def initialize
  yield self if block_given?
  @env ||= ::Vagrant::Environment.new
  define
end

Instance Method Details

#defineObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/zayin/rake/vagrant/php.rb', line 34

def define
  namespace :vagrant do
    namespace :php do
      desc <<-EOS
  Run phpunit on a PHP file.
    base_dir    The directory on the VM to run the phpunit in.
    phpunit_xml The phpunit.xml file relative to base_dir to configure the
        phpunit run.
    target      The class or PHP file relative to base_dir to run the tests on.
    coverage    The directory in the VM to put the HTML coverage reports into.
        EOS
      task :unit, [:base_dir,
                   :phpunit_xml,
                   :target,
                   :coverage] do |t, args|
        base_dir    = args[:base_dir] || '/vagrant'
        phpunit_xml = args[:phpunit_xml]
        target      = args[:target]
        coverage    = args[:coverage]

        opts = []
        opts << " -c #{phpunit_xml}" unless phpunit_xml.nil?
        opts << " --coverage-html #{coverage}" unless coverage.nil?
        opts << " #{target}" unless target.nil?

        cmd = "cd #{base_dir} && phpunit#{opts.join(' ')}"
        vm_ssh(cmd, coverage)
      end

      desc <<-EOS
  Run phpdoc in a directory.
    base_dir    The directory to run phpdoc from.
    output_dir  The output directory.
        EOS
      task :doc, [:base_dir, :output_dir] do |t, args|
        base_dir   = args[:base_dir]   || '/vagrant'
        output_dir = args[:output_dir] || '/vagrant/docs'

        cmd = "phpdoc -o HTML:frames:earthli -d #{base_dir} -t #{output_dir} " +
          "-i tests/,dist/,build/"
        vm_ssh(cmd, output_dir)
      end

      desc <<-EOS
  Run PHP Mess Detector in a directory.
    base_dir    The directory to run phpmd from.
    output_dir  The output directory.
    ruleset     An optional ruleset to also test against. This is added to
        the defaults of 'codesize,design,naming,unusedcode'.
        EOS
      task :md, [:base_dir, :output_dir, :ruleset] do |t, args|
        base_dir   = args[:base_dir]   || '/vagrant'
        output_dir = args[:output_dir] || '/vagrant/phpmd'
        ruleset    = args[:ruleset]    || ''

        ruleset = ',' + ruleset unless ruleset.empty?

        cmd = "phpmd #{base_dir} html codesize,design,naming,unusedcode#{ruleset} " +
          "--reportfile #{output_dir}/index.html"
        vm_ssh(cmd, output_dir)
      end

      desc <<-EOS
  Create PHP_Depend static code analysis report.
    base_dir    The directory to analyze.
    output_dir  The output directory.
        EOS
      task :depend, [:base_dir, :output_dir] do |t ,args|
        base_dir   = args[:base_dir]   || '/vagrant'
        output_dir = args[:output_dir] || '/vagrant/pdepend'

        cmd = "pdepend --jdepend-xml=#{output_dir}/jdepend.xml " +
        "--jdepend-chart=#{output_dir}/dependencies.svg " +
        "--overview-pyramid=#{output_dir}/overview-pyramid.svg " +
        "#{base_dir}"
        vm_ssh(cmd, output_dir)
      end

      desc <<-EOS
  Generate a PHP Copy/Paste Detection report.
    base_dir    The directory to analyze.
    output_dir  The output directory.
        EOS
      task :cpd, [:base_dir, :output_dir] do |t, args|
        base_dir   = args[:base_dir]   || '/vagrant'
        output_dir = args[:output_dir] || '/vagrant/phpcpd'

        cmd = "phpcpd --log-pmd #{output_dir}/pmd-cpd.xml #{base_dir}"
        vm_ssh(cmd, output_dir)
      end

      desc <<-EOS
  Generate a PHP_CodeSniffer report for coding standards.
    base_dir    The directory to analyze.
    output_dir  The output directory.
    standard    The standard to check against (default is Zend).
    args        Other arguments (default is none).
        EOS
      task :cs, [:base_dir, :output_dir, :standard, :args] do |t, args|
        base_dir   = args[:base_dir]   || '/vagrant'
        output_dir = args[:output_dir] || '/vagrant/phpcs'
        standard   = args[:standard]   || 'Zend'
        more_args  = args[:args]       || ''

        cmd = "phpcs --report=checkstyle " +
          "--extensions=php " +
          "--ignore=*/tests/* " +
          "--report-file=#{output_dir}/checkstyle.xml " +
          "--standard=#{standard} " +
          more_args +
          " #{base_dir}"
        vm_ssh(cmd, output_dir)
      end
    end
  end
end

#vm_ssh(cmd, output_dir) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/zayin/rake/vagrant/php.rb', line 16

def vm_ssh(cmd, output_dir)
  puts ">>> #{cmd}"
  unless output_dir.nil?
    @env.primary_vm.channel.execute(
      "if [ ! -d #{output_dir} ]; then mkdir -p #{output_dir}; fi"
    )
  end
  @env.primary_vm.channel.execute cmd do |channel, data|
    out = $stdout
    if channel == :stderr
      out = $stderr
    end

    out.write(data)
    out.flush()
  end
end