TinyMCE의 jstrim으로 js파일들을 통합하려고하니 다음의 문제가 있었습니다.
- 합쳐야할 js파일들의 목록을 xml파일로 따로 관리해야한다.
- 에러나는 경우 어디에서 에러가 발생하는 지 알수가 없다.
- (그리고 개인적인 사정) C#을 봐야 소스를 볼 수 있다. (-_- 귀차니즘~)
이러한 이유로 루비로 뚝딱뚝딱 프로그램을 만들어 보았습니다. 프로그램의 동작은 단순합니다.
- php파일을 읽어들입니다.
- <? if($isDebug) { ?> 에서부터 <?} else {?>까지의 script태그를 가진 라인들에서 src의 주소를읽어 들입니다.
- <?} else {?> 에서부터 <?} ?>까지의 script태그를 읽어들입니다.
- 2의 파일목록들을 3의 파일에 합쳐서 배포합니다.
아주 단순한 프로그램입니다만 짜는 과정에서 다음과 같은 에러로 고생을 했습니다.
- String 변수와 String 변수를 +연산자로 합치는 경우 (변수가 nil 이 될 때 에러가 나더군요.)
- Ruby 프로그램은 Block없이 프로그램을 짤 수 없다.
- File.open()으로 파일을 여는 경우 인자로 받는 경로가 절대 경로가 아니면 에러가 발생한다.(File.expand_path()로 절대 경로를 알 수 있다.
이런 노력(?)끝에 허접 프로그램이 나왔습니다. 돌기는 잘 도는 군요 - .-)//
허접이지만.. 오랜만에 포스팅을 위해서 올려봅니다.
more..
#@since 2007.07.10
#@author okjungsoo
#@license CCL
#
#<? if($isDebug) { ?>
# <script type="text/javascript" src="a.js"></script>
# <script type="text/javascript" src="b.js"></script>
# <script type="text/javascript" src="c.js"></script>
#<?} else {?>
# <script type="text/javascript" src="merged.js"></script>
#<?} ?>
# isDebug 사이에 있는 파일들을 else 뒤의 js파일로 통합시켜서 파일로 만들어 줍니다.
class FileMerger
FILE_ADDRESS = "..."
WWW_ADDRESS = "..."
attr_reader :srcScripts
attr_reader :dstScript
def initialize(fileName)
@fileName = fileName
parseHTML()
mergeFiles()
end
def parseHTML()
mode = -1, @srcScripts = []
File.open(@fileName, "r") do |file|
while line = file.gets
if line.index("") != nil
mode = 1
elsif mode == 1 and line.index("") == nil
line = getFileLocation(line)
if line != nil
@srcScripts.push(line)
end
elsif mode == 1 and line.index("") != nil
mode = 2
elsif mode == 2 and line.index("") == nil
@dstScript = getFileLocation(line)
elsif mode == 2 and line.index("") != nil
mode = -1
end
end
end
end
def getFileLocation(str)
dst = nil
startIndex = str.index("src=\"")
if startIndex != nil
startIndex = startIndex +5;
endIndex = str.index("\"", startIndex +5)
dst = str.slice(startIndex, endIndex - startIndex)
dst = dst.gsub(WWW_ADDRESS, FILE_ADDRESS)
end
return dst
end
def mergeFiles
file = File.open(File.expand_path(@dstScript), "w+")
@srcScripts.each do |rsc|
IO.foreach(File.expand_path(rsc)) {|line| file.print(line)}
end
file.close()
end
end
merger = FileMerger.new("...sample.php")
댓글 없음:
댓글 쓰기