Package question

Started by Thantos, January 11, 2005, 01:58:22 AM

Previous topic - Next topic

Thantos

I'm writing a mod that will require a custom action and I foresee the possibility of writting other mods that could take custom actions also.  So to reduce the number of files created I want to put all the non template stuff in one source file (Say $sourcedir/MikeMill.php) and then put all the template stuff in another file ($themedir/MikeMill.template.php) and any language stuff in its own ($themedir/languages/MikeMill.english.php)

Creating the files and getting them to interact isn't that hard.  What I want to know is how smart is the package manager?  Can it look to see if a file exists and if so do one action if not do another?  That way I can see if $sourcedir/MikeMill.php exists, if it does run the modification for it, if not copy over the file from the package.  Now if need be the file it copies can just be an outline one that defines the bare mins and then runs the modification on it.

In the package SDK I noticed the <require-file> tag but I couldn't see anything that explained exactly how it worked.

Thanks

[Unknown]

Yes, you're meant to be able to do this.  However, I'm afraid that the package manager could have used more thorough testing, so it may not be strictly possible.  However, *please note* that if you use this methodology, you may have to add multiple modification files (I would recommend it) per mod, because uninstallation will get more complicated.

Okay, so, what I would do is first do a search like this:

<file name="$sourcedir/MikeMill.php" error="ignore">
   <operation error="ignore">
      <search position="replace" regexp="true">^$</search>
      <add><![CDATA[<?php

// Some basic code for MikeMill.php...

// This is a comment to search for to add more!

?>]]></add>
   </operation>
</file>

And that should be one modification file - it won't be uninstallable, not realy.  Then, you have another one.  First, you do a "don't install me twice" check:

<file name="$sourcedir/MikeMill.php" error="fatal">
   <operation error="required">
      <search>function MyCoolMod()</search>
   </operation>
   <operation>
      <search position="before">// This is a comment to search for to add more!</search>
      <add><![CDATA[

// Das mod!]]></add>
   </operation>
</file>

Done ;).  This one should be uninstallable.  What do you think?  Tell me if this works, though, because it should - that's how it was designed.

-[Unknown]

Thantos

That method worked.  A couple things I noted was that it'll require a special uninstall as just reversing won't work, which isn't that big of a deal.
Is there a: find this, find that, remove everything in between?  That way for the uninstall I can just go
<search>function MyMod()</search>
<search>// End of MyMod</search>
<!-- remove between -->

How does
<file name="$sourcedir/MikeMill.php" error="fatal">
   <operation error="required">
      <search>function MyCoolMod()</search>
   </operation>
protect against multiple install?  Because I need to add an entry into index.php so that ?action=MikeMill would be handled but it only has to be included once since all the mods would be subactions.

I tried to do   <file name="$boarddir/index.php" error="fatal">
    <operation error="required">
      <search>      'MikeMill" => array('MikeMill.php', 'MikeMill_Main'),
</search>
    </operation>
    <operation>
      <search position="before">      '.xml' => array('News.php', 'ShowXmlFeed'),
</search>
      <add>      'MikeMill' => array('MikeMill.php', 'MikeMill_Main'),
</add>
    </operation>
  </file>

Which gave me weird results:  On the first mod it didn't change it, on the second mod it installed it twice.  Should this go into a seperate file from this (that was above it)
  <file name="$sourcedir/MikeMill.php" error="ignore">
    <operation error="ignore">
      <search position="replace" regexp="true">^$</search>
      <add><![CDATA[<?php
   // This files contains code needed for ?action=mikemill to work
   
   // Add code here
?>]]></add>
    </operation>
  </file>
<file name="$boarddir/index.php" error="fatal">
  <operation error="required">
    <search>      'MikeMill" => array('MikeMill.php', 'MikeMill_Main'),
</search>
  </operation>
  <operation>
    <search position="before">      '.xml' => array('News.php', 'ShowXmlFeed'),
</search>
    <add>      'MikeMill' => array('MikeMill.php', 'MikeMill_Main'),
</add>
  </operation>
</file>

Thanks

Thantos

#3
Ok the problem with the index.php insertion was a copy paste mistake.  For some reason I copied it twice into the second mod and none in the first one.

Now it inserts the change in index.php in both mods.

Futher Testing:

The "don't install me twice" check did not work.  Here is the installation file:
<?xml version="1.0"?>
<!DOCTYPE modification SYSTEM "http://www.simplemachines.org/xml/modification">

<modification xmlns="http://www.simplemachines.org/xml/modification" xmlns:smf="http://www.simplemachines.org/">
  <id>MikeMill:TestMod2</id>
  <version>0.1</version>

  <file name="$sourcedir/MikeMill.php" error="fatal">
    <operation error="required">
      <search>function MikeMill()_mod2</search>
    </operation>
    <operation>
      <search position="after"> // Add code here</search>
      <add><![CDATA[
function MikeMill_mod2()
{
die("I'm in mod2.  Thanks for testing");
}
]]></add>
    </operation>
  </file>
</modification>


Now if I understood you correctly if "function MikeMill_mod2()" exists then it shouldn't install.  However when I added it (manually) to MikeMill.php and then installed it it did put the function in the file.

[Unknown]

Sorry for not responding quickly.

Did you get a "failure" warning, though?  Or none?  Does it help to move the error attribute to "search" instead of "operation", maybe?

-[Unknown]

Thantos

#5
No warnings.  I'll try moving the error attribute to search and will edit this post with the result.

Ok latest attempt:
framework_install.xml
<?xml version="1.0"?>
<!DOCTYPE modification SYSTEM "http://www.simplemachines.org/xml/modification">

<modification xmlns="http://www.simplemachines.org/xml/modification" xmlns:smf="http://www.simplemachines.org/">
  <id>MikeMill:TestMod1</id>
  <version>0.1</version>

  <file name="$sourcedir/MikeMill.php" error="ignore">
    <operation error="ignore">
      <search position="replace" regexp="true">^$</search>
      <add><![CDATA[<?php
// This files contains code needed for ?action=mikemill to work

// Add code here
?>]]></add>
    </operation>
  </file>

<file name="$boarddir/index.php">
  <operation>
    <search error="required"> 'MikeMill" => array('MikeMill.php', 'MikeMill_Main'),
</search>
  </operation>
  <operation>
    <search position="before"> '.xml' => array('News.php', 'ShowXmlFeed'),
</search>
    <add> 'MikeMill' => array('MikeMill.php', 'MikeMill_Main'),
</add>
  </operation>
</file>
</modification>

Results:  If file MikeMill.php doesn't exist its create.  If it exists it doesn't do anything.  All good there.
In index.php I get an error message from the package manager if the 'MikeMill' => line exists or not.  When clicking proceed it will still add that to the array even if it already exists.  This isn't a huge deal since its just overwriting itself but I consider it messy :)

install.xml (only for test1)
<?xml version="1.0"?>
<!DOCTYPE modification SYSTEM "http://www.simplemachines.org/xml/modification">

<modification xmlns="http://www.simplemachines.org/xml/modification" xmlns:smf="http://www.simplemachines.org/">
  <id>MikeMill:TestMod1</id>
  <version>0.1</version>

  <file name="$sourcedir/MikeMill.php" error="fatal">
    <operation error="fatal">
      <search error="required">function MikeMill_mod1()</search>
    </operation>
    <operation>
      <search position="after"> // Add code here</search>
      <add><![CDATA[
function MikeMill_mod1()
{
die("I'm in mod1.  Thanks for testing");
}
]]></add>
    </operation>
  </file>
</modification>

Results: Got a failure message for function MikeMill_mod1() line wasn't there,  a success message if it was.  Both still installed it

install.xml (test1 again but attempt 2)
<?xml version="1.0"?>
<!DOCTYPE modification SYSTEM "http://www.simplemachines.org/xml/modification">

<modification xmlns="http://www.simplemachines.org/xml/modification" xmlns:smf="http://www.simplemachines.org/">
  <id>MikeMill:TestMod1</id>
  <version>0.1</version>

  <file name="$sourcedir/MikeMill.php" error="ignore">
    <operation error="fatal">
      <search error="required">function MikeMill_mod1()</search>
      <search position="after"> // Add code here</search>
      <add><![CDATA[
function MikeMill_mod1()
{
die("I'm in mod1.  Thanks for testing");
}
]]></add>
    </operation>
  </file>
</modification>

Results:  Failure message if function MikeMill_mod1() doesn't exist, doesn't install mod.  Failure message if it does exist, doesn't install mod.

[Unknown]

Yes, the package manager currently still tries to install if things fail but you click proceed anyway.

-[Unknown]

Thantos

But shouldn't it see that the operation failed on the first search and stop trying to process that operation?

[Unknown]

I'm afraid not.  Currently, it naively assumes you would never, in any state of mind, click proceed if it even thought of mentioning something failed.

-[Unknown]

Thantos

Can you check to see if the error="required" really does mean that an error is required for the operation to be successful?  I currently believe it does not operate in such a fashion.

[Unknown]

Should be:

<operation error="required">

-[Unknown]

Advertisement: