!

! $Id: set_copyright_year.pvc.pvt 3093 2022-06-16 16:27:03Z fmcguirk $

! (c) Copyright 2006-%(copyright-year)%, Sage Software Canada Ltd. (Ontario, Canada)

/*

 * Build information:

! ** @Author Fred McGuirk

! ** @Date Nov 22, 2007

! ** @OutputFile set_copyright_year.pvc

 

! ** This observer will change the value for copyright year prior to creating a new

! ** build of an application.

! **

! ** This program searches for '%(copyright-year)%' within the first 100 characters

! ** of the source file and will replace it with the current year.  It will then

! ** create a temporary file to be used as an alternative source file which will be

! ** deleted during the PostProcess of the build by this same observer.

 */

 

def class "set_copyright_year"

    like "EventManagerObserver"

 

    ! Override values for description / NotificationFlag

    local theDescription$= \

        "Substitute modification year of source file for 'Copyright-year' tag."

    local theNotificationFlag=_pvxConstants'_idePrePostProcess

 

    ! ** Private property to track if source file was modified

    local isModified

 

    /*

    ! ** The logic to be executed when the observer is triggered.

    ! ** @param initPvxState A reference to an object of class %PvxClass(PvxState)%

     */

    function update(initPvxState)                                    update

 

    /*

    ! ** Private function to do the work; used to keep the example simple

    ! ** @param initPvxState A reference to an object of class %PvxClass(PvxState)%

    ! ** @param outFile$ Name of output file to be created if source  modified

    ! ** @returns [boolean] True if source modified

     */

    function local update_copyright_year(initPvxState,outFile$)        update_copyright_year

end def

 

 

/*

 * The update routine will be executed by the event manager during the

 * pre/post process logic depending on how this observer registers

 * itself through the getEventNotification() method.

 */

update:

 

    enter aPvxState

 

    local psMajor$,psMinor$,enfState

 

    ! Get the Major/Minor codes that identify the current action

    psMajor$=aPvxState'getMajor$(), \

    psMinor$=aPvxState'getMinor$()

 

    ! Get the current state of processing for the ProvideX Event Manager

    enfState=aPvxState'getArgumentValue(_pvxConstants'_iEventNotificationFlag$)

 

    /*

     * This observer is for the program build event only; all other events

     * should be ignored.

     */

    switch psMajor$ + "|" + psMinor$

 

    ! Build a ProvideX program

    case _pvxConstants'Incremental_Build$ + "|" + _pvxConstants'BuildType_BuildOne$

    case _pvxConstants'Incremental_Build_Alt_Exe$ + "|" + _pvxConstants'BuildType_BuildOne$

 

        switch enfState

 

        case _pvxConstants'_idePreProcess

            isModified = _obj'update_copyright_year(aPvxState, outFile$)

 

            ! Set the current source file to the new output file

            if isModified {

                _obj'setCurrentSource(aPvxState, outFile$)

            }

 

            break

 

        case _pvxConstants'_idePostProcess

 

            ! write status message

            oReqClient = NEW( "pvxrequestclient")

            oReqClient'print(_obj'getDescription$()+": " + tbl(isModified, "no change", "updated"))

            drop object oReqClient

 

            break

        end switch

 

        break

    end switch

 

return 0

 

 

update_copyright_year:

 

    enter (aPvxState), altSrcFile$

 

    local retVal, reqSocket$, dest$, source$, sourceFN, fileSize, utcMtime, \

            theFile$, found, x, tempFN, retVal

 

    /*

     * Read the source file, check the first 100 characters for the

     * copyright year tag, replace with the current year, create a temporary

     * file and write the new source into it.

     */

    source$=aPvxState'getArgumentValue$(_pvxConstants'SrcFile$)

    open input (hfn,isz=1,err=*next)source$;

    sourceFN=lfo

 

    if sourceFN {

        fileSize=num(fin(sourceFN,"records_used")), \

        utcMtime=num(fin(sourceFN,"UTC_MTime"),err=*next)

 

        ! Use year from modification time of source file

        if utcMtime>0 then \

            utcMtime=int(utcMtime/86400)

        read (sourceFN,siz=fileSize+1,err=*next)theFile$

        close (sourceFN);

        sourceFN=0

 

        if not(nul(theFile$)) {

            ! Check the first 1000 characters of the source file for the tag

            found=pos("%(copyright-year)%"=lcs(mid(theFile$,1,1000)))

            if found {

                ! Substitute the modification year in place of the tag

                theFile$= \

                    mid(theFile$,1,found-1)+dte(utcMtime:"%Y")+ \

                    mid(theFile$,found+18)

 

                /*

                 * Get a temporary file to be used as new source

                 */

                x=new("pvx_utility")

                tempSourceFile$=x'getWorkFile$("WRK_set_copyright_date")

                drop object x

 

                ! Create a new temporary file

                serial tempSourceFile$,err=*next;

                open lock (hfn,err=*next)tempSourceFile$;

                tempFN=lfo

 

                ! Update the information for the temporary source file

                if tempFN {

                    ! Write the information to the temporary source file

                    write record (tempFN,err=*next)theFile$;

                    close (tempFN)

                }

                retVal = 1

            }

        }

    }

 

    return retVal

 

end