User forums > Help

Wizard script and variables expansion

(1/1)

ValeV:
Hello,

I am creating new wizard script for generating C header files. I want to add date to the top of the file (where [DATE] is in the code below). I tried to use variable expansion $(TODAY), but CodeBlocks crashes on creating new such header file.

wizard.script:

--- Code: ---////////////////////////////////////////////////////////////////////////////////
//
// Code::Blocks new file wizard script
//
// Project: C/C++ header file
//
////////////////////////////////////////////////////////////////////////////////

header_contents <- @"/**************************************************************************//**
 * \file
 *
 * \brief
 *
 * \author
 *
 * \version
 *
 * \date [DATE]
 *
 *
 ******************************************************************************/

#ifndef IG_[GUARD]
#define IG_[GUARD]

/*========================== function prototypes =================================*/



#endif // IG_[GUARD]
";

function BeginWizard()
{
    // this is the text that will appear in the start (intro) page
    local intro_msg = _T("Welcome to the new C/C++ header file wizard!\n" +
                         "This wizard will guide you to create a new C/C++ header file.\n\n" +
                         "When you 're ready to proceed, please click \"Next\"...");

    // add builtin pages
    //Wizard.AddInfoPage(_T("HFileIntro"), intro_msg); // intro
    Wizard.AddFilePathPage(true); // select filename (header guard required for header files)

    Wizard.SetFilePathSelectionFilter(_T("C/C++ header files (*.h;*.hpp;*.hxx;*.hh)|*.h;*.hpp;*.hxx;*.hh"));
}

function CreateFiles()
{
    local fname = Wizard.GetFileName();
    local ed    = GetEditorManager();
    if (IsNull(ed))
    {
        ShowError(_T("The wizard could not locate the editor manager."));
    }

    local ed_new = ed.New(fname);
    if (IsNull(ed_new))
    {
        ShowError(_T("The wizard could not create a new file.\n" +
                     "Maybe the target folder is write-protected?"));
    }
    else
    {
        // succeeded -> add header guard
        local guard = Wizard.GetFileHeaderGuard();
        local text = _T(header_contents);
        local auto_text = ed_new.GetText();

        text.Replace(_T("[DATE]"), "$(TODAY)");

        text.Replace(_T("[GUARD]"), guard);
        //text.Replace(_T("AUTO_GENERATED_CONTENTS"), auto_text);
        ed_new.SetText(text);

        // succeeded -> add file to project if needed
        if (Wizard.GetFileAddToProject())
        {
            AddFileToTargets(Wizard, fname);
        }
    }
    return fname;
}

--- End code ---

Is there a different way for inserting current date?

Thanks!

oBFusCATed:
Where does it crash (backtrace/callstack)?
What version are you using?
What is the exact wizard script and steps to reproduce?

ValeV:

--- Quote from: oBFusCATed on July 19, 2021, 09:17:01 pm ---Where does it crash (backtrace/callstack)?

--- End quote ---

When I try to create new header file with this custom script at the last step, when I click "Finish" in the wizard.


--- Quote from: oBFusCATed on July 19, 2021, 09:17:01 pm ---What version are you using?

--- End quote ---

Latest, CodeBlocks 20.03.


--- Quote from: oBFusCATed on July 19, 2021, 09:17:01 pm ---What is the exact wizard script and steps to reproduce?

--- End quote ---

I copy pasted the exact wizard script to the first message. Steps to reproduce is 1. create new custom script, 2. add it to global registration script, 3. create new header file with this custom script.

If I remove
--- Code: ---text.Replace(_T("[DATE]"), "$(TODAY)");
--- End code ---
from my script it works fine, so it looks clear where the problem is. So my question is how to insert current date to wizard script.

oBFusCATed:
Using a night build gives this error:
--- Code: ---Extracting 'PK8wxString' in 'wxString_Replace' failed for index 3
--- End code ---
And to decipher - this means that you've passed a string where wxString is expected. To fix it you have to use the _T("...") wrapper.
But even if you do fix this, it won't do want you want, because you need to use the macro manager to replace "$(today)" with the actual date.
You have to use something like:

--- Code: ---text.Replace(_T("[DATE]"), ReplaceMacros(_T("$(TODAY)")));
--- End code ---
No idea, if it works in 20.03.

ValeV:
Thank you! Your code works perfect on 20.03 also.  :)

Navigation

[0] Message Index

Go to full version