207 lines
5.6 KiB
Plaintext
207 lines
5.6 KiB
Plaintext
(*
|
||
* The contents of this file are subject to the Netscape Public License
|
||
* Version 1.1 (the "License"); you may not use this file except in
|
||
* compliance with the License. You may obtain a copy of the License at
|
||
* http://www.mozilla.org/NPL/
|
||
*
|
||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||
* for the specific language governing rights and limitations under the
|
||
* License.
|
||
*
|
||
* The Original Code is Mozilla Communicator client code, released March
|
||
* 31, 1998.
|
||
*
|
||
* The Initial Developer of the Original Code is Netscape Communications
|
||
* Corporation. Portions created by Netscape are Copyright (C) 1999
|
||
* Netscape Communications Corporation. All Rights Reserved.
|
||
*
|
||
* Contributors:
|
||
* Samir Gehani <sgehani@netscape.com>
|
||
*)
|
||
|
||
--// PUBLIC //--
|
||
|
||
property noErr : 0
|
||
property unknownErr : -100
|
||
property createErr : -101
|
||
property rdErr : -102
|
||
property replaceErr : -103
|
||
property wrErr : -104
|
||
|
||
property kEmptyStream : ""
|
||
property kDebug : false
|
||
property kVersionToken : "$Version$"
|
||
|
||
------------------------------------------------------------------------------------------
|
||
-- genJSFile
|
||
--
|
||
-- Generates a JS file from a supplied template after replacing all
|
||
-- predefined tokens with input parameters.
|
||
--
|
||
-- @param aSrcJSTFile the source JavaScript template file
|
||
-- @param aDstJSFolder the destination folder into which to generate the install.js
|
||
-- [NOTE: no ':' at path end]
|
||
-- @param aVersion the version string with which to replace the $Version$ string
|
||
-- @return err the error code as prescribed above
|
||
------------------------------------------------------------------------------------------
|
||
on genJSFile(aSrcJSTFile, aDstJSFolder, aVersion)
|
||
|
||
set err to unknownErr
|
||
set dstJSFile to aDstJSFolder & ":install.js"
|
||
|
||
-- read in source file
|
||
set inStream to readFileAsStream(aSrcJSTFile)
|
||
if inStream = kEmptyStream then
|
||
return rdErr
|
||
end if
|
||
|
||
-- perform substiution in text stream
|
||
set outStream to substitute(aVersion, inStream)
|
||
if outStream = kEmptyStream then
|
||
return replaceErr
|
||
end if
|
||
|
||
-- write altered text stream to dest file
|
||
set err to writeStreamToFile(dstJSFile, outStream)
|
||
|
||
-- return error val
|
||
return err
|
||
end genJSFile
|
||
|
||
|
||
--// PRIVATE //--
|
||
|
||
---------------------------------------------------------------------
|
||
-- createNewFile
|
||
---------------------------------------------------------------------
|
||
on createNewFile(aFile)
|
||
|
||
tell application "Finder"
|
||
-- delete old dest file
|
||
if file aFile exists then
|
||
delete file aFile
|
||
end if
|
||
|
||
-- create a new file
|
||
return open for access file aFile with write permission
|
||
end tell
|
||
end createNewFile
|
||
|
||
---------------------------------------------------------------------
|
||
-- readFileAsStream
|
||
---------------------------------------------------------------------
|
||
on readFileAsStream(aFile)
|
||
|
||
set inStream to kEmptyStream
|
||
|
||
tell application "Finder"
|
||
if file aFile exists then
|
||
set refnum to open for access file aFile without write permission
|
||
else
|
||
return inStream -- return empty stream if we couldn't read
|
||
end if
|
||
end tell
|
||
|
||
ASSERT(refnum > 0)
|
||
set inStream to read refnum as text
|
||
close access refnum
|
||
|
||
-- assert stream isn't empty
|
||
ASSERT(kEmptyStream < (count inStream each character))
|
||
|
||
return inStream
|
||
end readFileAsStream
|
||
|
||
---------------------------------------------------------------------
|
||
-- substitute
|
||
---------------------------------------------------------------------
|
||
on substitute(aVersion, aInStream)
|
||
|
||
set outStream to kEmptyStream
|
||
set currIndex to 1
|
||
set currChar to ""
|
||
set skipCount to 0
|
||
set firstOffset to offset of kVersionToken in aInStream
|
||
|
||
repeat with currChar in (every text item of aInStream)
|
||
if skipCount > 0 then
|
||
set skipCount to skipCount - 1
|
||
else
|
||
|
||
if currIndex = firstOffset then
|
||
set outStream to outStream & aVersion
|
||
set skipCount to strlen(kVersionToken) - 1
|
||
else
|
||
set outStream to outStream & currChar
|
||
end if
|
||
end if
|
||
set currIndex to currIndex + 1
|
||
end repeat
|
||
|
||
return outStream
|
||
end substitute
|
||
|
||
---------------------------------------------------------------------
|
||
-- writeStreamToFile
|
||
---------------------------------------------------------------------
|
||
on writeStreamToFile(aFile, aOutStream)
|
||
|
||
ASSERT(aOutStream kEmptyStream)
|
||
set err to noErr
|
||
|
||
set refnum to createNewFile(aFile)
|
||
ASSERT(refnum > 0)
|
||
|
||
write aOutStream to refnum
|
||
|
||
close access refnum
|
||
return err
|
||
end writeStreamToFile
|
||
|
||
---------------------------------------------------------------------
|
||
-- strlen
|
||
---------------------------------------------------------------------
|
||
on strlen(aString)
|
||
|
||
return count aString each character
|
||
end strlen
|
||
|
||
---------------------------------------------------------------------
|
||
-- DEBUG
|
||
---------------------------------------------------------------------
|
||
on DEBUG(aMsg)
|
||
|
||
if kDebug then
|
||
display dialog "GenJSLib DEBUG: " & aMsg
|
||
end if
|
||
end DEBUG
|
||
|
||
---------------------------------------------------------------------
|
||
-- ASSERT
|
||
---------------------------------------------------------------------
|
||
on ASSERT(aCondition)
|
||
|
||
if kDebug then
|
||
if not aCondition then
|
||
DEBUG("Assertion failed!")
|
||
quit -- come to a grinding halt
|
||
end if
|
||
end if
|
||
end ASSERT
|
||
|
||
---------------------------------------------------------------------
|
||
-- in place test harness for debugging
|
||
---------------------------------------------------------------------
|
||
on run
|
||
|
||
if kDebug then
|
||
set zFolder to "HD:genJS"
|
||
set src to zFolder & ":install.jst"
|
||
set dst to zFolder
|
||
set ver to "5.0.0.99288"
|
||
|
||
set err to genJSFile(src, dst, ver)
|
||
display dialog "[TEST] genJSFile returned: " & err
|
||
end if
|
||
end run |