mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-14 04:19:12 +00:00
Now using -script from CLI can return an error code (exit code != 0)
This commit is contained in:
parent
a20d55a0aa
commit
d6c6b00b32
@ -215,7 +215,7 @@ App::App(AppMod* mod)
|
||||
m_instance = this;
|
||||
}
|
||||
|
||||
void App::initialize(const AppOptions& options)
|
||||
int App::initialize(const AppOptions& options)
|
||||
{
|
||||
#ifdef ENABLE_UI
|
||||
m_isGui = options.startUI() && !options.previewCLI();
|
||||
@ -319,10 +319,13 @@ void App::initialize(const AppOptions& options)
|
||||
delegate.reset(new DefaultCliDelegate);
|
||||
|
||||
CliProcessor cli(delegate.get(), options);
|
||||
cli.process(&m_modules->m_context);
|
||||
int code = cli.process(&m_modules->m_context);
|
||||
if (code != 0)
|
||||
return code;
|
||||
}
|
||||
|
||||
os::instance()->finishLaunching();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void App::run()
|
||||
|
@ -84,7 +84,7 @@ namespace app {
|
||||
// Runs the Aseprite application. In GUI mode it's the top-level
|
||||
// window, in console/scripting it just runs the specified
|
||||
// scripts.
|
||||
void initialize(const AppOptions& options);
|
||||
int initialize(const AppOptions& options);
|
||||
void run();
|
||||
|
||||
AppMod* mod() const { return m_mod; }
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2018 Igara Studio S.A.
|
||||
// Copyright (C) 2018-2019 Igara Studio S.A.
|
||||
// Copyright (C) 2016-2018 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -33,8 +33,10 @@ namespace app {
|
||||
virtual void loadPalette(Context* ctx, const CliOpenFile& cof, const std::string& filename) { }
|
||||
virtual void exportFiles(Context* ctx, DocExporter& exporter) { }
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
virtual void execScript(const std::string& filename,
|
||||
const Params& params) { }
|
||||
virtual int execScript(const std::string& filename,
|
||||
const Params& params) {
|
||||
return 0;
|
||||
}
|
||||
#endif // ENABLE_SCRIPTING
|
||||
};
|
||||
|
||||
|
@ -178,7 +178,7 @@ CliProcessor::CliProcessor(CliDelegate* delegate,
|
||||
m_exporter.reset(new DocExporter);
|
||||
}
|
||||
|
||||
void CliProcessor::process(Context* ctx)
|
||||
int CliProcessor::process(Context* ctx)
|
||||
{
|
||||
// --help
|
||||
if (m_options.showHelp()) {
|
||||
@ -529,7 +529,9 @@ void CliProcessor::process(Context* ctx)
|
||||
// --script <filename>
|
||||
else if (opt == &m_options.script()) {
|
||||
std::string filename = value.value();
|
||||
m_delegate->execScript(filename, scriptParams);
|
||||
int code = m_delegate->execScript(filename, scriptParams);
|
||||
if (code != 0)
|
||||
return code;
|
||||
}
|
||||
// --script-param <name=value>
|
||||
else if (opt == &m_options.scriptParam()) {
|
||||
@ -598,6 +600,7 @@ void CliProcessor::process(Context* ctx)
|
||||
else {
|
||||
m_delegate->batchMode();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool CliProcessor::openFile(Context* ctx, CliOpenFile& cof)
|
||||
|
@ -32,7 +32,7 @@ namespace app {
|
||||
public:
|
||||
CliProcessor(CliDelegate* delegate,
|
||||
const AppOptions& options);
|
||||
void process(Context* ctx);
|
||||
int process(Context* ctx);
|
||||
|
||||
// Public so it can be tested
|
||||
static void FilterLayers(const doc::Sprite* sprite,
|
||||
|
@ -35,8 +35,10 @@ public:
|
||||
void saveFile(Context* ctx, const CliOpenFile& cof) override { }
|
||||
void exportFiles(Context* ctx, DocExporter& exporter) override { }
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
void execScript(const std::string& filename,
|
||||
const Params& params) override { }
|
||||
int execScript(const std::string& filename,
|
||||
const Params& params) override {
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool helpWasShown() const { return m_helpWasShown; }
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2018 Igara Studio S.A.
|
||||
// Copyright (C) 2018-2019 Igara Studio S.A.
|
||||
// Copyright (C) 2016-2018 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -131,11 +131,13 @@ void DefaultCliDelegate::exportFiles(Context* ctx, DocExporter& exporter)
|
||||
}
|
||||
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
void DefaultCliDelegate::execScript(const std::string& filename,
|
||||
const Params& params)
|
||||
int DefaultCliDelegate::execScript(const std::string& filename,
|
||||
const Params& params)
|
||||
{
|
||||
if (!App::instance()->scriptEngine()->evalFile(filename, params))
|
||||
throw std::runtime_error("Error executing script");
|
||||
auto engine = App::instance()->scriptEngine();
|
||||
if (!engine->evalFile(filename, params))
|
||||
throw base::Exception("Error executing script %s", filename.c_str());
|
||||
return engine->returnCode();
|
||||
}
|
||||
#endif // ENABLE_SCRIPTING
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2018 Igara Studio S.A.
|
||||
// Copyright (C) 2018-2019 Igara Studio S.A.
|
||||
// Copyright (C) 2016-2018 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -22,8 +22,8 @@ namespace app {
|
||||
void loadPalette(Context* ctx, const CliOpenFile& cof, const std::string& filename) override;
|
||||
void exportFiles(Context* ctx, DocExporter& exporter) override;
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
void execScript(const std::string& filename,
|
||||
const Params& params) override;
|
||||
int execScript(const std::string& filename,
|
||||
const Params& params) override;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
@ -214,8 +214,8 @@ void PreviewCliDelegate::exportFiles(Context* ctx, DocExporter& exporter)
|
||||
}
|
||||
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
void PreviewCliDelegate::execScript(const std::string& filename,
|
||||
const Params& params)
|
||||
int PreviewCliDelegate::execScript(const std::string& filename,
|
||||
const Params& params)
|
||||
{
|
||||
std::cout << "- Run script: '" << filename << "'\n";
|
||||
if (!params.empty()) {
|
||||
@ -224,6 +224,7 @@ void PreviewCliDelegate::execScript(const std::string& filename,
|
||||
std::cout << " " << kv.first << "=\"" << kv.second << "\",\n";
|
||||
std::cout << " }\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif // ENABLE_SCRIPTING
|
||||
|
||||
|
@ -34,8 +34,8 @@ namespace app {
|
||||
const std::string& filename) override;
|
||||
void exportFiles(Context* ctx, DocExporter& exporter) override;
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
void execScript(const std::string& filename,
|
||||
const Params& params) override;
|
||||
int execScript(const std::string& filename,
|
||||
const Params& params) override;
|
||||
#endif // ENABLE_SCRIPTING
|
||||
|
||||
private:
|
||||
|
@ -391,8 +391,15 @@ bool Engine::evalCode(const std::string& code,
|
||||
if (s)
|
||||
onConsolePrint(s);
|
||||
ok = false;
|
||||
m_returnCode = -1;
|
||||
}
|
||||
else {
|
||||
// Return code
|
||||
if (lua_isinteger(L, -1))
|
||||
m_returnCode = lua_tointeger(L, -1);
|
||||
else
|
||||
m_returnCode = 0;
|
||||
|
||||
// Code was executed correctly
|
||||
if (m_printLastResult) {
|
||||
if (!lua_isnone(L, -1)) {
|
||||
@ -407,6 +414,7 @@ bool Engine::evalCode(const std::string& code,
|
||||
catch (const std::exception& ex) {
|
||||
onConsolePrint(ex.what());
|
||||
ok = false;
|
||||
m_returnCode = -1;
|
||||
}
|
||||
|
||||
// Collect script garbage.
|
||||
@ -420,6 +428,9 @@ bool Engine::evalFile(const std::string& filename,
|
||||
std::stringstream buf;
|
||||
{
|
||||
std::ifstream s(FSTREAM_PATH(filename));
|
||||
// Returns false if we cannot open the file
|
||||
if (!s)
|
||||
return false;
|
||||
buf << s.rdbuf();
|
||||
}
|
||||
std::string absFilename = base::get_absolute_path(filename);
|
||||
|
@ -89,12 +89,17 @@ namespace app {
|
||||
onConsolePrint(text);
|
||||
}
|
||||
|
||||
int returnCode() const {
|
||||
return m_returnCode;
|
||||
}
|
||||
|
||||
private:
|
||||
void onConsolePrint(const char* text);
|
||||
|
||||
lua_State* L;
|
||||
EngineDelegate* m_delegate;
|
||||
bool m_printLastResult;
|
||||
int m_returnCode;
|
||||
};
|
||||
|
||||
class ScopedEngineDelegate {
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2019 Igara Studio S.A.
|
||||
// Copyright (C) 2001-2016 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -71,12 +72,14 @@ int app_main(int argc, char* argv[])
|
||||
|
||||
// Change the name of the memory dump file
|
||||
{
|
||||
std::string filename = app::memory_dump_filename();
|
||||
const std::string filename = app::memory_dump_filename();
|
||||
if (!filename.empty())
|
||||
memoryDump.setFileName(filename);
|
||||
}
|
||||
|
||||
app.initialize(options);
|
||||
const int code = app.initialize(options);
|
||||
if (code != 0)
|
||||
return code;
|
||||
|
||||
if (options.startShell())
|
||||
systemConsole.prepareShell();
|
||||
|
Loading…
x
Reference in New Issue
Block a user