aboutsummaryrefslogtreecommitdiffstats
path: root/mksymlinks
blob: fee8192c96e777f11ed52965a35b383442399053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
#!/usr/bin/env php
<?php
/**
 * @author Laurent Bachelier <laurent@bachelier.name>
 */
require(dirname(__FILE__).'/lib/bootstrap.php');
require(dirname(__FILE__).'/lib/FileTools.php');
require(dirname(__FILE__).'/lib/MultiConfig.php');
require(dirname(__FILE__).'/lib/Symfony.php');
require(dirname(__FILE__).'/lib/Argument.php');
require(dirname(__FILE__).'/lib/Color.php');

/**
 * @pram string $project_path Absolute project path
 * @param string $target The destination of the symlink
 * @param string $link The relative path of the symlink to create
 * @param boolean $relative Try to use a relative destination
 * @return boolean Success
 *
 * @author Laurent Bachelier <laurent@bachelier.name>
 */
function replace_symlink($project_path, $target, $link, $relative = true)
{
  if ($relative)
  {
    $target = FileTools::calculateRelativeDir($project_path.'/'.$link, $target);
  }

  FileTools::mkdirs(dirname($project_path.'/'.$link));
  $success = FileTools::symlink($target, $project_path.'/'.$link);

  log_message('  '.$link.' => '.$target.($success ? '' : ' ...FAILED!'), !$success);
}

/**
 * Find plugins with a "web" directory
 * @param string $project_path
 * @return array Plugin names
 *
 * @author Laurent Bachelier <laurent@bachelier.name>
 */
function find_plugins($project_path)
{
  $plugins = array();
  foreach (new DirectoryIterator($project_path."/plugins") as $file)
  {
    $name = $file->getFilename();
    if ($file->isDir()
        && preg_match('/^[^\.].+Plugin$/', $name)
        && is_dir($project_path.'/plugins/'.$name.'/web')
    )
    {
      $plugins[] = $name;
    }
  }

  return $plugins;
}


$options = MultiConfig::get();
$options['color'] = !Argument::get('C', 'no-color', false) && posix_isatty(STDOUT);
if ($options['color'])
{
  Color::enable();
}

$project_path = Symfony::getProjectPath();

log_message(Color::style('bright') . Color::fgColor('green')
  . 'Using symfony version ' . Color::style('normal')
  . Color::fgColor('yellow') . $options['want'] . Color::style('normal'));

$symlinks = array();
if ($options['genconf'])
{
  $symlinks[$options['genconf']] = $options['path'].'/genconf';
}

$sf_path = $options['sf_path'][$options['want']];
foreach (array(
    'symfony_symlink' => '',
    'lib_symlink' => 'lib',
    'data_symlink' => 'data',
    'web_symlink' => 'data/web/sf',
  )
  as $option => $relpath)
{
  $link = $options[$option];
  if ($link)
  {
    $target = $sf_path.'/'.$relpath;
    if (!is_dir($target))
    {
      throw new Exception($target.' is not a directory');
    }
    $symlinks[$link] = $target;
  }
}

log_message(Color::style('bright') . Color::fgColor('green')
  . 'Creating required directories...' . Color::style('normal'));
FileTools::mkdirs($project_path.'/cache');
FileTools::mkdirs($project_path.'/log');

log_message(Color::style('bright') . Color::fgColor('green')
  . 'Creating symbolic links...' . Color::style('normal'));
foreach ($symlinks as $link => $target)
{
  replace_symlink($project_path, $target, $link, $options['relative']);
}
if ($options['do_plugins'])
{
  if (version_compare($options['want'], '1.2') >= 0)
  {
    log_message(Color::style('bright') . Color::fgColor('green')
      . 'Creating symbolic links for plugins... (calling symfony)'
      . Color::style('normal'));
    if (empty($options['php_cmd']))
    {
      $options['php_cmd'] = PosixTools::which('php');
    }
    system(trim($options['php_cmd'].' symfony plugin:publish-assets'));
  }
  else
  {
    log_message(Color::style('bright') . Color::fgColor('green')
      . 'Creating symbolic links for plugins... (internal method)'
      . Color::style('normal'));
    foreach (find_plugins($project_path) as $name)
    {
      $link = 'web/'.$name;
      $target = $project_path.'/plugins/'.$name.'/web';
      // Ignore if there is a real directory with this name
      if (is_link($project_path.'/'.$link) || !is_dir($project_path.'/'.$link))
      {
        replace_symlink($project_path, $target, $link, $options['relative']);
      }
    }
  }
}