Skip to content

Commit

Permalink
#58: optimized link resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
ypujante committed Jul 6, 2013
1 parent e1c05ab commit def99b6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,27 @@ public class BasePackager

void resolveLinks(Resource destination)
{
def out = []
GroovyIOUtils.eachChildRecurse(inputPackage) { Resource file ->
if(file.filename.endsWith('.jar.lnk'))
resolveLink(destination, file)
resolveLink(destination, file, out)
}

if(out)
{
// optimization => cp forks and makes the process slow => forking once...
def content = """#!/bin/bash
${out.join('\n')}
"""

shell.withTempFile { Resource f ->
shell.saveContent(f, content)
shell.exec(f)
}
}
}

void resolveLink(Resource destination, Resource link)
void resolveLink(Resource destination, Resource link, def out)
{
Resource linkFolder = link.parentResource

Expand All @@ -90,9 +104,11 @@ public class BasePackager
String destinationPath =
inputPackage.file.toPath().relativize(linkFolder.file.toPath()).toString()

shell.cp(linkSource, destination.createRelative(destinationPath))
//shell.cp(linkSource, destination.createRelative(destinationPath))
out << "cp -R ${linkSource.file.canonicalPath} ${destination.createRelative(destinationPath).file.canonicalPath}"

shell.rm(destination.createRelative(destinationPath).createRelative(link.filename))
//shell.rm(destination.createRelative(destinationPath).createRelative(link.filename))
out << "rm ${destination.createRelative(destinationPath).createRelative(link.filename).file.canonicalPath}"
}

void processConfigs(Resource fromFolder, Map tokens, Resource toFolder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import javax.management.ObjectName
import javax.management.remote.JMXConnectorFactory
import javax.management.remote.JMXServiceURL
import java.nio.file.Files
import java.nio.file.NotDirectoryException
import java.security.MessageDigest
import java.util.concurrent.TimeoutException
import java.util.regex.Pattern
Expand Down Expand Up @@ -1167,6 +1168,84 @@ def class ShellImpl implements Shell
{
GluGroovyLangUtils.noException(closure)
}

/**
* Copy from to to...
* TODO MED YP: overriding filesystem method for speed issue
*
* @return to as a resource
*/
Resource cp(from, to)
{
copyOrMove(from, to, _copyAction)
}

/**
* Move from to to... (rename if file)
* TODO MED YP: overriding filesystem method for speed issue
*
* @return to as a resource
*/
Resource mv(from, to)
{
copyOrMove(from, to, _moveAction)
}

private def _copyAction = { Resource from, Resource to ->
exec(command: ['cp', '-R', from.file.canonicalPath, to.file.canonicalPath])
}

private def _moveAction = { Resource from, Resource to ->
exec(command: ['mv', from.file.canonicalPath, to.file.canonicalPath])
}

/**
* Copy or move... same code except ant action
*
* @return to as a resource
*/
Resource copyOrMove(from, to, Closure action)
{
from = toResource(from)

if(!from.exists())
throw new FileNotFoundException(from.toString())

def toIsDirectory = to.toString().endsWith('/')
to = toResource(to)
toIsDirectory = toIsDirectory || to.isDirectory()

if(from.isDirectory())
{
// handle case when 'from' is a directory

// to is an existing file => error
// cp -R foo foo4
// cp: foo4: Not a directory
if(!toIsDirectory && to.exists())
throw new NotDirectoryException(to.toString())
}
else
{
// handle case when 'from' is a file

// to is a non existent directory => error
// cp foo4 foo8/
// cp: directory foo8 does not exist
if(toIsDirectory && !to.exists())
throw new FileNotFoundException(to.toString())
}

// to is an existent directory => copy inside directory
if(toIsDirectory)
to = to.createRelative(from.filename)

mkdirs(to.parentResource)

action(from, to)

return to
}
}

class MBeanServerConnectionCategory
Expand Down

0 comments on commit def99b6

Please sign in to comment.